Rails 4.0 : Selected value for form_for [select] - ruby-on-rails

When submitting the below form, parameters are available in the controller with params[:teachers], but in the view (same view is rendered), selected value for "language_id" isn’t params[:teacher][:language_id], but the default one (first option).
<%= form_for :teacher , :url => {:action =>"search_teacher"} , :html => { :method => "post"} do | f | %>
<%= f.select :language_id , t('languages_hash'), :include_blank => false %>
<%= f.submit :value => t("search_button") %>
When debugging with the debug method in the view, « params[:teacher][:language_id] » parameter is present.
Thank you

By what I understood from your question, you are trying to get the previously selected language to remain selected after the form submission. For that you may need to pass a selected option in the f.select.
<%= f.select("Language", "language_id", Language.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true, :selected => params[:language_id] }) %>
This may do the trick.
For more option you can go through this link

I'm guessing that t('languages_hash') isn't returning data in the proper format that the select tag is expecting.
Check out the docs - I'm guessing you'll want to wrap that in an options_for_select call, and possibly change the order of what is returned from t('languages_hash').

Related

Get selected value from select_tag and pass it as a parameter

I have a separate select_tag and a link_to and I'm sending some parameters via the link_to. I want to get the selected value and send it as one of the parameters. I tried to add selected: params[:whatever] to the select_tag, that didn't work. I tried doing it via js, I can get the selected value but I'm not sure how to set to that specific parameter.
As requested:
<%= select_tag :shipping_opt, options_for_select(delivery_opt(#select_store)), :include_blank => true %>
<%= link_to 'Use this address', checkouts_step_two_path(:data => Checkout::CRYPT.encrypt_and_sign(s.id), :store => Checkout::CRYPT.encrypt_and_sign(#select_store.id), :order_info => ?? ) %>
the order_info should be the selected shipping opt.
Assuming you retrieve values from the select_tag with a format similar to this:
value = $('#selectList').val();
I'd assume that you just set this to a variable and put it into the params hash in the link_to
<%= link_to 'Use this address', checkouts_step_two_path(:data => Checkout::CRYPT.encrypt_and_sign(s.id), :store => Checkout::CRYPT.encrypt_and_sign(#select_store.id), **:order_info => value** ) %>

form_for submits some extra params which are not part of that object?

I want to submit some extra params which are not part of that model object. here is my code example
= form_for Comment.new, :url => notes_path({:pined_feed_item => ""}), :html => {class: 'form clearfix', id: 'add_post_form'}, remote: true do |form|
.control-group
= form.text_area :text, :placeholder => 'Post to the community', :cols => nil, :rows => nil, :class => 'mention expand-without-submit'
= check_box_tag :pined_feed_item
pinged post
= submit_tag "Post your message", class: 'btn btn-success btn-mini'
I want to get check_box value at the controller level, but when I inspect there it gives me empty string
It gives you an empty string because you haven't set any value for it. Try using check_box_tag the way it is described in the API. The second argument (pined) is the value of the checkbox and the third one is for the state of the checkbox (if it's going to be checked or not). For example:
...
check_box_tag 'pined_feed_item', 'pined', true
# => <input checked="checked" id="pined_feed_item" name="pined_feed_item" type="checkbox" value="pined" />
...
In this case the parameters array in the controller will be something like:
# params.inspect
"...{:pined_feed_item=>"pined"}..."
So you can do checks in your controller based on this condition
if params[:pined_feed_item] == 'pined'
...
end
Remove {:pined_feed_item => ""} from your url. That is overwriting the value of checkbox pined_feed_item.
When you check the box and submit the form you will see that pined_feed_item"=>"1", if unchecked and submitted then this parameter will not be part of params hash.
Hope, this helps.
How are you accessing your extra params?
It should be available as params[:pined_feed_item].
If you like to access them with params[:comment][:pined_feed_item], then you need to change your view to check_box_tag 'comment[pined_feed_item]'.

How do I set a selected option in this Rails 2 selection form?

I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>

How can I make this RESTful when it's requested with few parameters at once?

I made my routing just like this. But what if genre was empty?
Isn't it going to redirect to example.com/shops/newest_first//california?
How can I solve this kind of routing and parameters problem??
routes.rb
match 'shops/:sort/:genre/:area', :to => 'shops#index'
view
<%= form_tag shops_path, :method => :get do %>
<%= select_tag :sort, options_from_collection_for_select(Sort.all, 'id', 'name', params[:sort]), :prompt => "Newest first" %>
<%= select_tag :genre, options_from_collection_for_select(Genre.all, 'id', 'name', params[:genre]), :prompt => "all" %>
<%= select_tag :area, options_from_collection_for_select(Area.all, 'id', 'name', params[:area]), :prompt => "all" %>
<% end %>
Another View
I would consider using GET params for things like area and sort since you are filtering indexes of other resources. You might also check the the section on Dynamic Segments in the guides, although that won’t help with the empty segment in the middle.

Using form_for multi-select fields with acts_as_taggable_on

I can't figure out the right code for using a predetermined set of options for a multi-select field. I want to have a list of skills in a drop down that users can select from. Here is the code I am using, it works fine as a single select field, but not as a multi-select:
<%= form_for(#user, :html => { :class => "form-stacked" } ) do |f| %>
...
<div class="clearfix"><%= f.select :skill_list, options_for_select(["Asst", "dir", "pres"]),
{
:multiple => true,
:class => "chzn-select",
:style => "width:450px;" } %></div>
...
<% end %>
Anyone have any suggestions? Eventually, I will want to store all of the options for the multi-select form elsewhere because there will be a bunch, but this is the first challenge I can't figure out..
Thanks.
EDIT
I have also tried:
:html => { :multiple => true, :class => "chzn-select", :style => "width:450px;" } and it doesnt work either
There needs to be two pairs of brackets, one for the options, and one for html_options, like so:
<%= f.select :skills_list, options_for_select(["Asst", "dir", "pres"]), {}, {:multiple => true, :class => "chzn-select", :style => "width:450px;" } %>
See the docs for the select helper.

Resources