How do I set the id attribute in my drop down boxes?
Here is the part of my form in question:
<%= f.fields_for :items do |builder| %>
<%= builder.label :description %><br />
<%= builder.text_field :description %><br />
<%= builder.label :material %><br />
<%= builder.select :material, #letters.map { |l| [l.material, l.material] }, :id => "material_field" %><br />
<%= builder.label :height %><br />
<%= builder.select :height, #letters.map { |l| [l.height, l.height] }, :id => "height_field" %><br />
<%= builder.label :thickness %><br />
<%= builder.select :thickness, #letters.map { |l| [l.thickness, l.thickness] }, :id => "thickness_field" %><br />
<%= builder.label :quantity %><br />
<%= builder.text_field :quantity, :id => "quantity_field" %>
<%= builder.link_to_remove "Remove this item" %>
<% end %>
The :id => "quantity_field" method works for text fields, but not for the select fields. Viewing the HTML source I am getting an id of "estimate_items_attributes_0_material" for the material text box.
This is a strange inconsistency. Any help will be greatly appreciated.
There is a parameter between the possible choices and the html options. So you have to do this :
<%= builder.select :thickness, #letters.map { |l| [l.thickness, l.thickness] }, {}, :id => "thickness_field" %>
You can find the doc here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
And this one can also be helpful :
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
Related
I am trying to figure out the cleanest way to create multiple DB entries at once (or if it's even a good idea).
I have an Event model, which has many Bands and many Venues, through the event_bands and event_venues models.
Right now, the user first creates the event, then is redirected to the form to create the event_band and event_venue relationships. I would like to streamline all of this into one form if possible, like this (bottom lines are most relevant):
<%= form_for #event do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :contact %>
<%= f.text_field :contact %><br />
<%= f.label :price %>
<%= f.text_field :price %><br />
<%= f.label :info %>
<%= f.text_area :info %><br />
<%= f.label :date %>
<%= f.date_select :date, start_year: 2014 %><br />
<%= f.label :time %>
<%= f.text_field :time, placeholder: "7:40pm" %><br />
<%= f.label :band %>
<%= select(:event_band, :band_id, options_for_select(Band.order(name: :asc).collect { |b| [b.name, b.id]} )) %><br />
<%= f.label :venue %>
<%= select(:event_venue, :venue_id, options_for_select(Venue.order(name: :asc).collect { |v| [v.name, v.id]} )) %><br />
<%= f.submit %>
<% end %>
Because the event_band and event_model require event.id, I guess I would have to use an after_create callback to build and save them. The issue I'm having conceptualizing this is that I want to post those extra :band_id and :venue_id params through the form for the event.
Is there a cleaner way to handle posting params for multiple models through one form and then using them in a callback or am I headed in the wrong direction?
You can use fields_for : see doc
I'am using select_tag and when I update selected value is not stored
when i update :name, :tag value get first option
edit.html.erb
<%= form_for(#name) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :lname%><br />
<%= f.text_field :lname%>
</div>
<div class="field">
<%= f.label :tag %><br />
<%= f.select :tag, "<option>1</option><option>2</option><option>3</option><option>4</option>".html_safe %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Solution
<%= f.collection_select(:tag, [1,2], :to_i, :to_s, :prompt => 'Categories') %>
Maybe?
= f.select :tag, (1..4).to_a, :prompt => '---'
try to use select tag from this link
select_tag
<%= select_tag "tag", "<option>1</option><option>2</option>" %>
Try this
<%= f.select "tag", "<option value="1">1</option><option value="2">2</option></select>".html_safe %>
read http://ashleyangell.com/2009/11/form-select-helper-in-ruby-on-rails/
Solution
<%= f.collection_select(:tag, [1,2], :to_i, :to_s, :prompt => 'Categories') %>
How I can make this form in activeadmin?
<%= form_for(#album, :html => {:multipart => true}) do |f| %>
.....
<div class="field">
<%= f.label :apellido %><br />
<%= f.text_field :apellido %>
</div>
<div class="field">
<p>Hijos</p>
<%= f.fields_for :hijos do |builder| %><br /><br />
<%= builder.label :nombre, 'Nombre Hijo' %><br />
<%= builder.text_field :nombre %><br />
<%= builder.label :apodo, 'Apodo Hijo' %><br />
<%= builder.text_field :apodo %><br />
<%= builder.label :hijo_id, 'favorito' %>
**<%= f.radio_button :hijo_id, builder.object.id %>**
<% end %>
</div>
I need put the option of hijo_id inside the for of :hijos
Try with :
f.input :avatar_item_id, :as => :boolean, :value => app_f.object.id
But not work.
Thanks
This should work once you register the Album model as an ActiveAdmin resource:
form :html => {:multipart => true} do |f|
f.inputs "Principal" do
f.input :apellido
end
f.inputs "Hijos" do
f.has_many :hijos do |h|
h.input :nombre
h.input :apodo
h.input :favorito, :as => :check_box
end
end
f.buttons
end
If you want to mark a child as a favorite, you need a boolean favorito field in the hijos table, not a hijo_id field.
My view:
<h1>New Address</h1>
<% form_for #address, :url => new_address_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Add Address" %>
<% end %>
Partial... nothing special
<%= form.label :number %><br />
<%= form.text_field :number %><br />
<br />
<%= form.label :street %><br />
<%= form.text_field :street %><br />
<br />
<%= form.label :city %><br />
<%= form.text_field :city %><br />
<br />
<%= form.label :state %><br />
<%= form.text_field :state %><br />
<br />
<%= form.label :zip_code %><br />
<%= form.text_field :zip_code %><br />
<br />
<br />
my error:
ActionController::MethodNotAllowed
Only get, put, and delete requests are allowed.
MethodNotAllowed seems to come from resource-based routing. Resource-based routing requires distinct method names to match actions. In your case, you should supply :method => :post in form_for, something like that:
<% form_for #address, new_address_path, :method => :post) do |f| -%>
I have a from like this:
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.label :role %> <br/>
<%= f.text_field :role%>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
In the database, role is a "Char" field. I want it different from a textfield, the user can select "Teacher", "Student", if the user select "Teacher", the database will store "T", otherwise, it will store "S". How can I do so? It is necessary for me to add a "User role" table in the database, and then make a relationship with the user? But it is necessary to do it this way? thank u.
Ref select and options_for_select
<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>