Adding 4 Drinks to a Station in Form - ruby-on-rails

I have two models: Stations & Drinks
Stations has_many :drinks and Drinks belongs_to :stations
I'm trying to make my form so that when you create a station, you can select 4 drinks that will belong to that station as well.
Original form:
<%= semantic_form_for [:admin, #station] do |f| %>
<fieldset class="inputs">
<ol>
<%=f.input :name %>
<%=f.input :number, :as => :number %>
</ol>
</fieldset>
<%= f.buttons :commit %>
I've been trying to figure out how to create 4 (select) input fields in this form so that you can select Drink #1, #2, #3, #4 for the current station. Any ideas?
I'm currently trying accepts_nested_attributes_for :drinks, :allow_destroy => true.
Drinks belongs_to :station
id | name | station_id |
Stations has_many :drinks
id | name |
.
UPDATE
As noted below by tihm, you can add 4 drinks and edit their values with this:
<% f.fields_for :drinks do |drink_form| %>
<%# Put your inputs here...could be a select box %>
<%= drink_form.select :name, [['Drink #1', 'drink_1'],['Drink #2', drink_2] %>
<%# Or a plain input %>
<%= drink_form.input :description %>
<%# ... Any other drink attributes ... %>
<% end %>
However, what I'm trying to do is generate four select boxes that each list Drink.all and be able to swap out one drink object with a different one. So, when you change the value of the first select box from coke to pepsi, it removes the station_id from coke, and adds the station_id to pepsi.
I don't need to be able to edit the drink attributes.. I just need to change which drinks are associated with this station. Is this possible in the same form?

You'll want to start by watching:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
If you know you'll always have 4 or less, then it's a bit easier and you can skip the js from the Railscasts.
In your controller, be sure to build the empty drink objects you need:
....
#station = Station.new
4.times do
#station.drinks.build
end
...
This way, the #fields_for in the view has objects to iterate over. In your view, something ilke:
<%= semantic_form_for [:admin, #station] do |f| %>
<fieldset class="inputs">
<ol>
<%=f.input :name %>
<%=f.input :number, :as => :number %>
<% f.fields_for :drinks do |drink_form| %>
<%# Put your inputs here...could be a select box %>
<%= drink_form.select :name, [['Drink #1', 'drink_1'],['Drink #2', drink_2] %>
<%# Or a plain input %>
<%= drink_form.input :description %>
<%# ... Any other drink attributes ... %>
<% end %>
</ol>
</fieldset>
<%= f.buttons :commit %>
The #select will depend a lot on what you are selecting from and the structure of that data. Is it coming from a model, a simple list, etc.

Unfortunately, I couldn't find a "best-practice" rails way of doing... so I ended up doing a bunch of AJAX / custom ruby code to implement it. Here it is (maybe it'll help someone else):
<% #drinks = Drink.all %>
<fieldset class="inputs">
<ol>
<% #station.drinks.each_with_index do |d,i| %>
<li>
<label class="label" for="station_name">Item</label>
<%=select("drink", "id", #drinks.collect { |d| [d.name,d.id] }, {:include_blank => true, :selected => d.id}, {:class => "station-items"})%>
</li>
<% end %>
<% m = 4-#station.drinks.count %>
<% m.times do %>
<li>
<label class=" label" for="station_name">Item</label>
<%=select("drink", "id", #drinks.collect { |d| [d.name,d.id] }, {:include_blank => true}, {:class => "station-items"})%>
</li>
<% end %>
</ol>
</fieldset>
<%= f.buttons :commit %>
<% end %>
<script>
$(".station-items").on("change",function(){
var node = $(this)
, prev = node.attr('data-rel')
, next = parseInt(node.val())
, station_id = $("#station_id").val()
if(next) {
$.ajax({
url: "/drinks/"+next+".json",
type: "PUT",
data: { id:next, "drink[station_id]":station_id }
});
}
if(prev) {
$.ajax({
url: "/drinks/"+prev+".json",
type: "PUT",
data: { id:prev, "drink[station_id]":"" }
});
}
$(this).attr('data-rel',next);
});
$('.station-items option:selected').each(function(){
return $(this).parent().attr('data-rel',$(this).val())
});
</script>

Related

Rails 4: text_field for acts_as_taggable_on not separating tags with a comma

I am trying to get the text_field in my form partial to comma-separate acts_as_taggable_on tags. Right now, when I reload the page, the commas disappear so if a field has two or more tags, they become one big tag instead. For instance, I get "Tag1 Tag2 Tag3" instead of "Tag1, Tag2, Tag3". I am using acts-as-taggable-on 3.4.2.
Here is my _form.html.erb partial:
<h2>Tags:</h2>
<p>Please separate the tags with a comma ','</p>
<% #article.tag_types.each do |tag| %>
<div class="form-group">
<strong><%= label_tag tag.to_s.titleize %></strong><br />
<%= f.text_field "#{tag.to_s.singularize}_list".to_sym, :placeholder => "Comma-separated list of #{tag.to_s}", class: 'form-control' %>
</div>
<% end %>
Every time I reload the edit page, the input value somehow removes the commas from the already-present tags, so the text field looks like this:
<input id="article_country_list" class="form-control" type="text" name="article[country_list]" value="China U.S.A." placeholder="Comma-separated list of countries">
instead of having value="China, U.S.A." as it should be.
Here is my model, article.rb:
class Article < ActiveRecord::Base
acts_as_taggable_on :people, :cities, :countries, :other
end
Any help would be much appreciated :)
Thanks!
Apparently this is a new security feature.
I solved the comma separation issue by doing:
<% #article.tag_types.each do |tag| %>
<div class="form-group">
<strong><%= f.label tag.to_s.titleize %></strong><br />
<% tag_sym = "#{tag.to_s.singularize}_list".to_sym %>
<% tag_list = "#{tag.to_s.singularize}_list" %>
<%= f.text_field tag_sym, value: #article.send(tag_list).to_s, :placeholder => "Comma-separated list of #{tag.to_s}", class: 'form-control' %>
</div>
<% end %>
Thanks! Since I am using ActiveAdmin with Formtastic I made a custom input.
So I created a new class: app/inputs/tag_list_input.rb with:
class TagListInput < Formtastic::Inputs::StringInput
def input_html_options
super.merge(:value => "#{#object.send(method).to_s.html_safe}")
end
end
and using this like:
f.input :some_tag_list, :as => :tag_list, :label => "SomeTags"

Ruby On Rails - validate nested params in controller (must not be nil)

These are my params:
{"utf8"=>"✓", "authenticity_token"=>"0RYiIDDgmOk0gCDRkAgHvv+UIgp/BuU33CLThJXqOTE=",
"order"=>
{"operation_in_orders_attributes"=>
{"0"=>{"service_operation_id"=>"5"},
"1"=>{"service_operation_id"=>""},
"2"=>{"service_operation_id"=>"4"},
"3"=>{"service_operation_id"=>""},
"4"=>{"service_operation_id"=>""}},
"kontakt"=>"comment", "Car_id"=>"50"},
"commit"=>"Dodaj",
"car_id"=>"dw815gn"}
Order has many operation_in_orders
Order has many service_operations through OperationInOrder
OperationInOrder belongs to Order
OperationInOrder belongs to ServiceOperation
ServiceOperation has many operation_in_orders
ServiceOperation has many orders through OperationInOrder
My form:
<%= form_for #order, url: new_car_order_path(#car, #order), html: {class: "add_order"} do |r| %>
<%= r.label "Service", class: :add_order_label %>
<% 5.times do %>
<%= r.fields_for :operation_in_orders do |v| %>
<%= v.collection_select(:service_operation_id, ServiceOperation.all, :id, :nazwa,include_blank: true) %>
<!-- <%= v.text_field :order_id, value: #order.id, :style => "display:none" %> -->
<% end %>
<% end %>
<%= r.label "Kontakt", class: :add_order_label %>
<%= r.text_field :kontakt %>
<%= r.text_field :Car_id, value: #car.id, :style => "display:none" %>
<%= r.label " " %>
<%= r.submit "Add", class: "sub" %>
<%= link_to "Send",ordered_path(car_id: #car.id) , class: 'sub'%>
<% end %>
I have a form where I can choose five ServiceOperations at most to an order and save.
When I save, 5 new OperationInService objects/rows are made.
Is there a possibility to not create those join tables if corresponding field on form is blank?
For example:
I fill only 2 from 5 fields. I save only these two, not 5. Now I save nil values...
I have tried to validate in OperationInService model, but there was an error (rails do not recognize format in controller).
Any ideas?
Update the accepts_nested_form_for method call in Order model as below:
class Order < ActiveRecord::Base
has_many :operation_in_orders
accepts_nested_attributes_for :operation_in_orders, reject_if: proc { |attributes| attributes['service_operation_id'].blank? }
## ..
end
This way record for operation_in_orders would not be created if service_operation_id is blank.

Rails check_box_tag within form_for

Is it possible to pass the value of checked check_box_tags within a form_for in Rails inside a hash?
Here is a very generic, basic version of the form:
<%= form_for(:object, :url => { :action => 'create', :id => params[:id]}) do |f| %>
<p>Field_a: <%= f.text_field(:field_a) %></p>
<p>Field_b: <%= f.text_field(:field_b) %></p>
<p>Field_c: <%= f.text_field(:field_c) %></p>
<p>Check boxes:</p>
<% check_box_choices_object_array.each do |s| %>
<%= check_box_tag(s.name, 1, false) %>
<%= .name %><br />
<% end %>
<%= submit_tag("Create") %>
<% end %>
Outputs roughly:
Field_a ___________________
Field_b ___________________
Field_c ___________________
Check boxes:
[] box_a
[] box_b
[] box_c
[] box_d
[] box_e
[] box_f
[] box_g
My problem is that since the available check boxes aren't actual fields in the object's table in the database (i.e. I'm not using check_box(:field) in the form), each checked check box gets passed as an individual parameter (i.e. "box_a" => "1", "box_b" => "1", "box_e" => "1"). I would like them to be passed as such:
:checked_boxes => {"box_a" => "1", "box_b" => "1", "box_e" => "1"}
This way, I can access them easily with params[:checked_boxes].
How do I do this, or, better yet, is there a better solution (I'm new to rails)?
I think you'd get the results you want if you wrap the checkboxes iterator in a fields_for :checked_boxes tag - or at least get you close to the results you want.
<%= form_for(:object, :url => { :action => 'create', :id => params[:id]}) do |f| %>
<p>Field_a: <%= f.text_field(:field_a) %></p>
<p>Field_b: <%= f.text_field(:field_b) %></p>
<p>Field_c: <%= f.text_field(:field_c) %></p>
<p>Check boxes:</p>
<%= f.fields_for :checked_boxes do |cb| %>
<% check_box_choices_object_array.each do |s| %>
<%= cb.check_box(s.name, 1, false) %>
<%= .name %><br />
<% end %>
<% end %>
<%= submit_tag("Create") %>
<% end %>
you can deal with no database attributes and models using attr_accessor
class Thing < ActiveRecord::Base
attr_accessible :name
attr_accessor :box_a, :box_b, :box_c
end
This way you can call these attributes in your form.

AssociationTypeMismatch: Expect a model but got an Array? Rails 3 nested forms

I have a nested form, and within that another one. Basically what I have is a bunch of Ingredients and for each ingredient I have a quantity and unit which is stored in an association table between Entry and Ingredient called EntryIngredient. With my following JS, I'm able to add dynamic fields, but when I submit, I'm getting AssociationTypeMismatch error. Not sure why, the parameters look good to me ("ingredients_attributes"=>{"0"=>{"name"=>"Salt", "entry_ingredients"=>{"quantity"=>"2.5", "unit"=>"tbspns"}, "_destroy"=>""}}}}), what am I missing? Actually, I think it might be because its sending as entry_ingredients instead of entry_ingredients_attributes, but I don't see what I did differently in the second nested form. Here is the first form that has the fields for ingredients:
<div id="ingredients">
<p>Ingredient List:</p>
<%= f.fields_for :ingredients, #entry.ingredients.build do |builder| %>
<%= render 'ingredient_fields', :f => builder %>
<% end %>
</div>
<div id='add_ingredient'>Add Ingredient</div>
<div class="actions">
<%= f.submit %>
here is the code for entry_ingredients:
<ul id="ingredient_list">
<li>
<%= f.label :name %>
<%= f.text_field :name, :class => "ingredient_field" %>
<%= f.hidden_field :_destroy, :class => "delete_ingredient" %>
<%= f.fields_for :entry_ingredients, #entry.entry_ingredients.build do |builder| %>
<%= render 'entry_ingredient_fields', :f => builder %>
<% end %>
<%= link_to "Remove", "#", :class => "remove_fields" %>
</li>
</ul>
and heres my JS for adding a new ingredient dynamically:
$('form').on('click', '#add_ingredient', function(){
count = $('#ingredient_list li').length;
field = $('#ingredient_list li').first()
.clone() //clone the first element in the list
.find('input') //find all its inputs
.val('') //set all input values to blank
.end()
.find($('.ingredient_field'))
.prop({id: 'entry_ingredients_attributes_' + count + '_name', name: 'entry[ingredients_attributes][' + count +'][name]' })
.end()
.find($('.delete_ingredient'))
.prop({id: 'entry_ingredients_attributes_' + count + '__destroy', name: 'entry[ingredients_attributes][' + count +'][_destroy]', value: 'false' })
.end()
.find($('.ingredient_quantity'))
.prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_quantity', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][quantity]'})
.end()
.find($('.ingredient_unit'))
.prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_unit', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][unit]'})
.end();
$('#ingredient_list').append(field);
})
The form helpers inspect the relationship between your models to figure out how to submit parameters. Did you include accepts_nested_attributes_for :entry_ingredients in the Ingredient model?

Multiple forms for the same model in a single page

On the front page of my rap lyrics explanation site, there's a place where users can try explaining a challenging line:
alt text http://dl.dropbox.com/u/2792776/screenshots/2010-02-06_1620.png
Here's the partial I use to generate this:
<div class="stand_alone annotation" data-id="<%= annotation.id %>">
<%= song_link(annotation.song, :class => :title) %>
<span class="needs_exegesis"><%= annotation.referent.strip.gsub(/\n/, "\n <br />") %></span>
<% form_for Feedback.new(:annotation_id => annotation.id, :created_by_id => current_user.try(:id), :email_address => current_user.try(:email)), :url => feedback_index_path, :live_validations => true do |f| %>
<%= f.hidden_field :annotation_id %>
<%= f.hidden_field :created_by_id %>
<p style="margin-top: 1em">
<%= f.text_area :body, :rows => 4, :style => 'width:96%', :example_text => "Enter your explanation" %>
</p>
<p>
<% if current_user %>
<%= f.hidden_field :email_address %>
<% else %>
<%= f.text_field :email_address, :example_text => "Your email address" %>
<% end %>
<%= f.submit "Submit", :class => :button, :style => 'margin-left: .1em;' %>
</p>
<% end %>
</div>
However, putting more than one of these on a single page is problematic because Rails automatically gives each form an ID of new_feedback, and each field an ID like feedback_body (leading to name collisions)
Obviously I could add something like :id => '' to the form and all its fields, but this seems a tad repetitive. What's the best way to do this?
If you don't want to change your input names or your model structure, you can use the id option to make your form ID unique and the namespace option to make your input IDs unique:
<%= form_for Feedback.new(...),
id: "annotation_#{annotation.id}_feedback"
namespace: "annotation_#{annotation.id}" do |f| %>
That way our form ID is unique, i.e. annotation_2_feedback and this will also add a prefix, e.g. annotation_2_, to every input created through f.
Did you consider nested_attributes for rails models? Instead of having multiple new feedback forms where each is tied to an annotation, you could have multiple edit annotation forms where each annotation includes fields for a new feedback. The id's of the generated forms would include the annotations id such as edit_annotation_16.
The annotation model would have a relationship to its feedbacks and will also accept nested attributes for them.
class Annotation < ActiveRecord::Base
has_many :feedbacks
accepts_nested_attributes_for :feedbacks
end
class Feedback < ActiveRecord::Base
belongs_to :annotation
end
You could then add as many forms as you want, one for each annotation. For example, this is what I tried:
<% form_for #a do |form| %>
Lyrics: <br />
<%= form.text_field :lyrics %><br />
<% form.fields_for :feedbacks do |feedback| %>
Feedback: <br/>
<%= feedback.text_field :response %><br />
<% end %>
<%= form.submit "Submit" %>
<% end %>
<% form_for #b do |form| %>
Lyrics: <br />
<%= form.text_field :lyrics %><br />
<% form.fields_for :feedbacks do |feedback| %>
Feedback: <br/>
<%= feedback.text_field :response %><br />
<% end %>
<%= form.submit "Submit" %>
<% end %>
And the quick and dirty controller for the above edit view:
class AnnotationsController < ApplicationController
def edit
#a = Annotation.find(1)
#a.feedbacks.build
#b = Annotation.find(2)
#b.feedbacks.build
end
def update
#annotation = Annotation.find(params[:id])
#annotation.update_attributes(params[:annotation])
#annotation.save!
render :index
end
end
I had this same issue on a site I'm currently working on and went with the solution you mention at the bottom. It's not repetitive if you generate the ID programmatically and put the whole form in a partial. For example, on my site, I have multiple "entries" per page, each of which has two voting forms, one to vote up and one to vote down. The record ID for each entry is appended to the DOM ID of its vote forms to make it unique, like so (just shows the vote up button, the vote down button is similar):
<% form_for [entry, Vote.new], :html => { :id => 'new_up_vote_' + entry.id.to_s } do |f| -%>
<%= f.hidden_field :up_vote, :value => 1, :id => 'vote_up_vote_' + entry.id.to_s %>
<%= image_submit_tag('/images/icon_vote_up.png', :id => 'vote_up_vote_submit' + entry.id.to_s, :class => 'vote-button vote-up-button') %>
<% end -%>
I also had the same issue but wanted a more extensible solution than adding the ID to each field. Since we're already using the form_for ... |f| notation the trick is to change the name of the model and you get a new HTML ID prefix.
Using a variant of this method: http://www.dzone.com/snippets/create-classes-runtime (I removed the &block stuff)
I create a new model that is an exact copy of the model I want a second form for on the same page. Then use that new model to render the new form.
If the first form is using
#address = Address.new
then
create_class('AddressNew', Address)
#address_new = AddressNew.new
Your ID prefix will be 'address_new_' instead of 'address_' for the second form of the same model. When you read the form params you can create an Address model to put the values into.
For those stumbling here, looking for the solution for Rails 3.2 app, look at this question instead:
Rails: Using form_for multiple times (DOM ids)

Resources