I have a Campaign model which I want the user to be able to check off and on a checkbox for each state, which I want to store in an array in one the fields, like ["CA", "NY", "OH", "FL"].
I'm not sure if serialized is the correct terminology here. I have some form fields generated like so:
<% State::NAMES.each do |state| %>
<%= check_box("states_allowed", state[1], :checked => true) %>
<label for="states_allowed_<%= state[1] %>"><%= state[0] %></label>
<% end %>
In which State::NAMES, is just an array of state names and abbreviations.
So this gives me the HTML of:
...
<input type="hidden" value="0" name="states_allowed[NE]"><input type="checkbox" value="1" name="states_allowed[NE]" id="states_allowed_NE">
<label for="states_allowed_NE">Nebraska</label>
...
But how can I save the check fields? The column content stays null and I don't receive an error.
Something I have tried is putting serialize :states_allowed in my Campaign model, but no luck.
I add the same problem today, it appears the form isn't properly built in the view.
I simply added at the beginning of my create action:
params[:campaign][:states_allowed] = params[:states_allowed]
And it works properly
Related
On a model, I have attr_accessor: :email_settings.
In a view, I have:
<%= form_for some_model do |f| %>
<%= f.fields_for :email_settings do |email_settings| %>
<%= email_settings.label :general, _("General updates") %>
<%= email_settings.check_box :general %>
General site updates
<% end %>
<% end %>
But in the HTML, this does not create one input for some_model[email_settings][general], it creates two. One hidden and one a checkbox:
<label for="user_email_settings_general">General updates</label>
<input name="user[email_settings][general]" type="hidden" value="0">
<input id="user_email_settings_general" name="user[email_settings][general]" type="checkbox" value="1">
General site updates
What's going on here? Why is there a hidden input and a checkbox for the same value, when I only want a checkbox?
Also in the controller action that the form is submitted to, I do this:
def update
puts "email_params: #{email_params}"
end
def email_params
params.require(:user).permit(:email_settings)
end
Which outputs:
Unpermitted parameters: email_settings
email_params: {}
Not sure how email_settings is being interpreted as "unpermitted" when I'm explicitly permitting it.
From the browser should always send a response.
If you do not select the box, will send the hidden field to understand that something has been sent.
In this way the rails will always receive an indication that the box was selected or not.
similar answer is here: Why does the check_box form helper generate two checkboxes, one hidden?
I am using geocomplete plugin to auto complete address form in my form.
Geocomplete only recognizes form fields with a set name attribute, such as
<input name="lat" class="lat" type="text" value="">
<label>Longitude</label>
<input name="lng" class="lng" type="text" value="">
<label>GPS</label>
<input name="location" type="text" value="">
other wise, it wont be able to find the field in a given form to complete.
below is the form in rails 4
<%= form_for(#property, :url => landlord_properties_path(current_landlord)) do |f| %>
<div class="form-group selection property type">
<%= f.text_field :name,class: "geocomplete" %>
<%= f.text_field :lng,class: "lng" %>
<%= f.hidden_field :lat,class: "lng" %>
<%= f.submit "add" %>
<% end %>
however the generated HTML has nested attibutes like
<input class="lng" id="property_lng" name="property[lng]" type="text">
which has the property added to the attribute, so geocomplete wont be able to find the form.
how do customize the name attribute to
name ="lng"
and still send the attribute to the current #property object and pass it into the controller?
EDIT
so the solution is to add 'name'=>'lng' to the text field, this works great!
however, now the attribute don't get past the strong parameter
here is the property_params
def property_params
params.require(:property).permit(:name,:lat,:lng)
end
and here is the what gets passed into the form parameter
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uXfT7XUZBuedbfAj8OaDMT673txmk379BM5PENQBRsk=", "property"=>{"name"=>"100 Kent Street, Ottawa, ON, Canada", "lat"=>""}, "lng"=>"-75.70343909999997", "commit"=>"add", "dependent"=>:destroy, "landlord_id"=>"1"}
it seems like property is a hash containing the values, but lng is now a seperate thing outside of the property hash, so it doesnt get written into the db
how do i set custom parameter to let lng into the property controller object?
Add this to f.text_field:
'name'=>"lng"
I have two models store and category. A store can have many categories and should get stored in categories_stores table. The model relationships are setup properly and I have the following on the store form:
f.input :categories, :as => :check_boxes
And they do indeed display correctly. But creating or editing a store doesn't create / delete those records in the categories_stores table.
Any ideas?
Update: The above code already generates the list correctly (see screenshot and HTML code below) -- the only problem is changes to this aren't getting saved in the DB!
<input id = "merchant_category_ids_" name="merchant[category_ids][]" type="hidden" value="" />
<ol>
<li><input id="merchant_category_ids_1" name="merchant[category_ids][]" type="checkbox" value="6" /> Clothing</li>
<li><input checked="checked" id="merchant_category_ids_2" name="merchant[category_ids][]" type="checkbox" value="5" /> Electronics</li>
</ol>
Sounds like you may be trying to set sub-resources. If that's the case, you can try something like:
<%= f.fields_for :categories do |f_categories| %>
<%= f_categories.input ... %>
<% end %>
Take a look at the fields_for doc for more info.
I know this is already answered, but have you made sure that the following is defined in the respective models?
attr_accessible :categories_stores_attributes
accepts_nested_attributes_for :categories_stores
My intention is to display 7 checkboxes, 1 for each day of the week, and save it in a single field in a table, in CSV format. (So, Sunday,Wednesday would be saved as string 0,3)
So, I am able to get the CSV of the selected week days into the field.
But when I edit the form, all the checkboxes are unchecked.
How do I get the checkboxes to be selected, based upon the CSV in the field?>
This is how I am showing the checkboxes in the form:
<% Date::DAYNAMES.each_with_index do |day,index| %>
<%= check_box_tag "post[week_days_#{index}]", index, false,
:name => "post[week_days][]" %>
<% end %>
Output is:
...
<input id="post_week_days_0" name="post[week_days][]" type="checkbox" value="0" />
<input id="post_week_days_1" name="post[week_days][]" type="checkbox" value="1" />
...
I think I have to change the form like below (added the :checked option):
<%= check_box_tag "post[week_days_#{index}]", index, false,
:name => "post[_week_days][]",
:checked => (post.week_days.include? index ? true : false) %>
But how will I get the include? to work, since the field is a string, and I somehow need that string as an array?
Ps. This is the way I am trying to implement this, but if there is some better way to do this, I could use that way..
You could do
:checked => (post.week_days.include? index.to_s)
You dont have to give ?(ternary) condition because .include? returns true or false
I currently have a form that will pass 2 parameters to my controller. My question is every time I make a choice in the select_tag form, I want my option to stay after I hit the submit tag. That way the user knows what he or she just selected. I could used :selected=>"true", but thats only for the default value and not for the value submitted.
<form name="filter" action="" style="display:inline" >
<label for="filter">Filter by Name or Description: </label>
<%= text_field_tag "query", params['query'] %>
<label for="status">Filter by Status:</label>
<%= select_tag(:sortstatus,
'<option value="empty">Show All</option>,
<option value="0">Applying</option>,
<option value="3">Suspended</option>,
<option value="4">Pending</option>') %>
<%= submit_tag 'Search' %>
</form>
And here is the controller that will change the value of empty to work with my table
def sort_status
if params[:sortstatus] == "empty"
#statusorder = ""
else #statusorder = params[:sortstatus]
end
end
Haven't been able to find any solution so far in Google.
Take a look at using options_for_select to generate your options tags. It allows you to specify which entry you would like to be selected. e.g.
<%= select_tag(:sortstatus, options_for_select([['Show All', 'empty'],
['Applying', '0'],
['Suspended', '3'],
['Pending', '4']], params[:sortstatus]) %>
This will set the selected item to the current value of params[:sortstatus]