I'm using mongoid, and i save the geo location info in an array field.
In my model, I have:
field :location, :type => Array
In my view, I have:
<%= f.hidden_field :location %>
And this gives the result:
<input id="foo_location" name="foo[location]" type="hidden">
What I want is like below:
<input id="foo_location_1" name="foo[location][]" type="hidden">
<input id="foo_location_2" name="foo[location][]" type="hidden">
In order for the location param to be passed back as an array, you have to explicitly define the input name for these fields:
<% #foo.location.each do |loc| %>
<%= f.hidden_field :location, :name => "foo[location][]", :value => loc %>
<% end %>
I have found the solution by setting :multiple => true, which will give the right result.
<%= f.hidden_field :location, :multiple => true, :id => "foo_location_1" %>
<%= f.hidden_field :location, :multiple => true, :id => "foo_location_2" %>
Related
Using Rails v5.0.0.1, and given a form:
<%= form_for(map, :html => { :multipart => true }) do |f| %>
<%# several inputs ... %>
<div class="form-group">
<%= f.label :longitude, t("label.longitude") %>
<%= f.number_field :longitude,
:class => "form-control",
:value => "0.000000",
:step => "0.000001",
:min => "-180.000000",
:max => "180.000000"
%>
</div>
<%# several inputs ... %>
<%= f.submit :class => "btn btn-primary" %>
</form>
The form submits, the controller kicks in, and the "magic" of rails happens.
Until the validation part.
At the model, I've attempted several different validators, from a simple validate block, to a validates_numericality_of.
As so:
validate do
errors.add(:longitude, 'bum') unless (-180.0..180.0).include?(longitude)
end
And even:
validates_numericality_of :longitude,
:greather_than_or_equal_to => -180.0,
:less_than_or_equal_to => 180.0
However, every value that I try ( 0, 0.000000, 180 ) says it's out of bounds.
What am I missing here ?
i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]
I have the following form_for - structure
<%= form_for Group.new, url: what_to_do_files_path ,method: :get ,:validate => true do |f| %>
<div class="field">
<%=f.text_field :group_name, placeholder: "Group Name" %>
</div>
<%= button_tag :class => "btn btn-primary", :name => 'submit' do %> Submit <% end %>
<button type="button" id="check_all"> Check / Uncheck All</button>
lalalala
<%end%>
model:
class Arraygroup < ActiveRecord::Base
attr_accessible :group_name, :user_id
no_whitespace=/^[\S]+$/
validates :group_name,
:format => { :with => no_whitespace} ,
:uniqueness => { :scope=> :user_id,:case_sensitive => false}
validates_length_of :group_name, :minimum => 3
end
So, I want to be able to client-side-validate the database for the group_name and user_id. The problem is that user can only specify the name of the group in a text field and the user_id is 0 then. I want to specify the user_id on my own somehow, for example
<% f.user_id => #user_spec_id%>, where #user_spec_id is predefined and points to the ID of the user. My version doesnt work of course. I have tried something like this as well
<% user_id => #user_spec_id%> but it was wrong.
Thanks in advance
You are looking for a hidden_field_tag
In your view, do the following,
<%= hidden_field_tag #{f.object_name}[user_id], #user_spec_id %>
Obviously inside your form_for.
When you submit the form, you will see that,
in your params["group"] hash, you added an user_id.
I'm using accepts_nested_attributes_for in one of my Rails app. i have three models :album has_many :photo and each :photo has_and_belongs_to_many :tag. User can add more photo in album using paperclip and jquery.multifile.js. every thing working fine but the main problem is the hash is not properly create.
album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}
but i need.
album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}
My view is: please consider the gallery => album and attachment => photo
<%= form_for(#gallery, :html => {:multipart => true}) do |f| %>
<div id = "gallery_name_formField">
<%= f.label "Gallery Name:" %><br />
<%= f.text_field :name, :name => "gallery[name]" %><br />
</div>
<div id = "gallery_body_formField">
<%= f.label "Gallery Description:" %><br />
<%= f.text_area :body, :name => "gallery[body]" %><br />
</div>
<%= f.fields_for :attachments do |af| %>
<div id = "gallery_file_formField">
<%= af.label "Select The Attachment:" %><br />
<%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
</div>
<div id="tags">
<%= af.fields_for :tags do |tf| %>
<%= tf.label :name, "Tag:" %><br />
<%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
<% end %>
</div>
<% end %>
<div class="attachment_submit_formField">
<%= f.submit(:id => "create_gallery") %>
</div>
<% end %>
Have you tried to use nested form gem? Give it a try
Its very straightforward way to have multiple nested forms.
It works for me.
I have this in the Main form
<%= simple_nested_form_for #customer_bill do |f| %>
<%= f.label :customer_id %>
<%= f.collection_select :customer_id, Customer.all,:id,:name, {:prompt => "Select Customer"}, :style => 'width:205px;' %><br />
<%= f.link_to_add "Add", :customer_bill_line_items, :locals => {:text_1 => :customer_id} %>
/* rest of code */
<%end%>
And i have this in my customer_bill_line_items Partial
<%= f.hidden_field :customer_id, :value => :text_1 %>
/*rest of code*/
But i am not Able to capture the selected customer id in the partial. The value of hidden field is coming as 0. Any guidance on how i can solve this matter will be great. Thanks in advance
<%= f.hidden_field :customer_id, :value => :text_1 %>
I am guessing that customer_id in an integer field, and you are setting the :value as text_1 which is a string.
if the value of text_1 is an integer then text_1.to_i