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

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -