Change html form id generated by form_for rails 3.1
Adding on to what miked said, the easiest way to make unique form id's for the posts would be to use the post's id numbers in the id attribute, like so:
<%= form_for [post, Comment.new,], :remote => true, :html => { :id => "new_comment_on_#{post.id}" } do |f| %>
Rails 3.1: Form_for content is displayed outside of form/form tags on index page
Silly mistake.
I had an open tag that was causing all the problems.
Use Rails’ form_for but set custom classes, attributes on form element?
Use the :html
hash:
= form_for @user, :html => {:class => 'x', 'data-bar' => 'baz'} do |f|
Or
= form_for @user, html: {class: 'x', data: { bar: 'baz' } } do |f|
Ruby on Rails: Are form_for(:product, ...) and form_for(@product, ...) equivalent?
The @product
in the form_for
helper ships with more features.
The :product
only affects the input field's id and name. For example you have a text filed in the form:
<%= form_for :product, :url => {...} do |f| %>
<%= f.text_field :price %>
<% end %>
The generated html would look like:
<input type="text" id="product_price" name="product[price]" />
The id
and name
value is determined by the :product.to_s
and the text field name.
While if you use @product
, the :url
is not necessary because the url would be determined according to the @product
's status:
- if the
@product
is a new record, the url would post tocreate
- otherwise, the url would post to
update
And the input filed's id and name is affected by @product
's class name, so it's important when you're using single table inheritant. The input filed's value is automatically assigned with the @product
's attribute value. So if you use @product
, the html output would look like:
<input type="text" id="product_price" name="product[price]" value="some value" />
Assume the @product
's class name is Item
, then the output would change to:
<input type="text" id="item_price" name="item[price]" value="some value" />
And of course you can use both of :product
and @product
:
<%= form_for :product, @product do |f| %>
The :product
controls input filed's name and id, and the @product
controls the url and input field's value.
Is it correct to specify :html = { :multipart = true} for form_for?
It's not required in Rails 3.1 and later, as long as you're using form_for
with file_field
like this:
<%= form_for @person do |f| %>
<%= f.file_field :picture %>
<% end %>
This will not work:
<%= form_for @person do |f| %>
<%= file_field_tag 'person[picture]' %>
<% end %>
You can easily verify that it's working. Inspect the generated HTML and look for the enctype="multipart/form-data"
attribute on the form tag. Rails doesn't do any magic beyond setting the encoding type, so if the attribute is there, you're good.
RoR field set on form_for
You can use field_set_tag
. For example, using a generic 'user' object
For Rails 2.3.x
<% form_for(@user) do |f| %>
<% field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
And for Rails 3.0.0:
<%= form_for(@user) do |f| %>
<%= field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
adding (not overwriting a class) for a form generated by the form_for helper
Following the comment's advice I did this via manual addition of the original class
Related Topics
Safe Navigation Equivalent to Rails Try for Hashes
Ruby on Rails User Registration Using Rest API Call and Devise
Is There a Ruby Http Client Library with a Response Cache
What Is the Point of Object#Presence in Rails
Ruby - Array.Join Versus String Concatenation (Efficiency)
Rails: Update Model Attribute Without Invoking Callbacks
Is There a Ruby One-Line "Return If X"
How to Detect the End of a Method Chain in Ruby
How to Determine Leap Year in Ruby
Get Time Object at Start of Day in a Particular Time Zone
Checking If a String Is Valid JSON Before Trying to Parse It
Active Admin: Sorting on Multiple Columns