ruby on rails - Assigning attributes in HTML form or when calling .new(*attrs) -
i have worked on numerous projects have seen both setting of attributes new object in html form , when calling model.new(foo: 'bar')
which correct way of doing in fashion of "best practices"?
form:
<%= form_for user.new, remote: true |f| %> <%= f.hidden_field :foo, value: "bar" %> <%= f.text_field :email %> <% end %>
instance variable:
#obviously set in controller @user = user.new(foo: "bar") <%= form_for @user, remote: true |f| %> <%= f.text_field :email %> <% end %>
at first case, when validation of user not passed, empty form rendered, @ second case form filled fields rendered. it's because instance variable @user @ controller keep entered values.
so recommend use second variant.
Comments
Post a Comment