Set a standard value for a form element
When you want a field to be used and a standard value is to be expected - you could prepopulate some fields beforehand. No need to evaluate expressions in your view, as this quickly makes a mess. Leave the logic to the controller.
# friends_controller.rb def new @friend = Friend.new @friend.is_on_facebook = true end
<%= nested_form_for(friend) do |f| %> <div class="field form-group"> <%= f.label :is_on_facebook %> <%= f.check_box :is_on_facebook, class: "form-control" %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
All done by simply setting the model’s corresponding attribute in the appropriate controller. The view remains untouched.
⬅️ Read previous Read next ➡️