selected values are resetting in page reload in ruby on rails - ruby-on-rails

Iam new to ruby on rails.I want to show the selected values from drop down and checked radio button in page reload.
When the page is reloaded, the selected value of the drop down and radio button are getting reset.So please let me know how to fix this issue.Here is the code i'am using.
<%= form_tag :action => 'show' do%>
<strong>Select device: </strong> <%= collection_select(:device, :id, #devices, :id, :name, options ={:prompt => "-Select a device"}) %>
<br></br>
<strong>Chose:</strong><%= radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph%>Graph
<% end %>

Try:
collection_select(:device, :id, #devices, :id, :name, {:selected => #your_selected_value, :prompt => "-Select a device"})
see radio_button_tag(name, value, checked = false, options = {})
<%= radio_button_tag :name,:time, #your_checked_value.eql?('time'), :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph, #your_checked_value.eql?('graph') %>Graph

Related

Rails: collection_check_boxes to filter search results

I'm trying to implement a search view which enables users to search other users based on keyword AND multiple checkboxes for their tags. I'm still trying to figure out how to place the collection_check_boxes right, so that view is rendered correctly. Here's what I have so far:
<%= form_tag users_path, :method => 'get' do %>
<%= collection_check_boxes :options, :tag_ids, Tag.order(:name), :id, :name, {multiple: true} %>
<%= text_field_tag :search, params[:search], :placeholder => "Search", :id => "query" %>
<%= submit_tag "Search", :name => nil, :style => "float:left;"%>
<% end %>
Can you help me complete the view function above by making sure that, when a user clicks search collection_check_boxes will add something like tag=tag1&tag2&tag3 to the url?
I will try :)
Generally everything is OK. Only small correction, if you change this line:
<%= collection_check_boxes :options, :tag_ids, Tag.order(:name), :id, :name, {multiple: true} %>
to this:
<%= collection_check_boxes :tag, :ids, Tag.order(:name), :id, :name, {multiple: true} %>
after submitting request, in URL will be something like this:
&tag%5Bids%5D%5B%5D=&tag%5Bids%5D%5B%5D=3&tag%5Bids%5D%5B%5D=2&search=test
more human view:
&tag[ids][]=&tag[ids][]=3&tag[ids][]=2&search=test
and this is OK. It always look like this when sending an array. In this particular case I've checked two checkboxes (with id=2 and 3). I don't know why there is also empty element :), but this is not a problem. I think there is no way to get results exactly like tag=tag1&tag2&tag3
In your "users" model you can get to the params by (after submitting form):
params[:tag][:ids] - array of id (checked checkboxes)
params[:search] - search input value
If you want to have checked some checkboxes on start (or after submit), add to collection_check_boxes additional parameter:
options = {:checked=>tag_ids}
and deliver in variable tag_ids array of checkboxes id (which should be checked). All the code looks like this:
<%if params.has_key?(:tag)%>
<%tag_ids = params[:tag][:ids]%>
<%else%>
<%tag_ids = Array.new%>
<%end%>
<%=form_tag drons_path, :method => 'get' do %>
<%=collection_check_boxes :tag, :ids, Tag.order(:name), :id, :name, {multiple: true}, options = {:checked=>tag_ids}%>
<%=text_field_tag :search, params[:search], :placeholder => "Search", :id => "query"%>
<%=submit_tag "Search", :name => nil, :style => "float:left;"%>
<%end %>

how to select all values by default of form_for select

In a form with form.select I want all options to be selected by default. I have age model, in form which is showing all ages I want to be selected all options by default. My code is following:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{},{:multiple=>true , :required=>true})
Try this:
f.select(:age, options_for_select(Age.pluck(:age, :id) , :selected => #campaign.ages.pluck(:id)),{:multiple=>true , :required=>true})
I did this through checks. If a user is created as a new record, then I selected all of them and in case of edit, I will only show only selected. As code following speaks of itself:
<% if #campaign.new_record? %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => Age.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% else %>
<%= f.select(:age, options_for_select(Age.pluck(:age, :id), :selected => #campaign.ages.pluck(:id) ), {}, {:multiple => true,:required => true ,:class => 'form-control'}) %>
<% end %>

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 would I reset my Chosen select menu after creating a resource?

How does one reset a Chosen plugin field after resource is created by Javascript?
I create a dog and can get the regular form fields to reset but not the select menu using the Chosen plugin:
dogs/create.js.erb
$('#dogs').prepend('<%= escape_javascript(render(#dog)) %>');
$('.add-dog-form > form')[0].reset();
This is successful reset, just not with the chosen menu which is called from my application.js:
jQuery( function($) {
// Chosen Select Menu
$('.category-select').chosen().trigger("liszt:updated");
});
Than my form:
<%= form_for(#dog, :remote => true) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :category, "Categories" %>
<%= f.select :category_ids, Category.all.collect {|c| [c.name, c.id]}, {}, { :multiple => true, :class => "category-select" } %>
<% end %>
So again, how does one reset a Chosen plugin field after resource is created by Javascript?
Try this:
dogs/create.js.erb
$('#dogs').prepend('<%= escape_javascript(render(#dog)) %>');
$('.add-dog-form > form')[0].reset();
$('.category-select').chosen().trigger("liszt:updated");
You basically have to trigger the reset again inside create.js.erb.

How do you make each option in a drop down menu a link with the association simple_form call?

I have this form using the simple_form plugin:
<%= simple_form_for([#video, #video.comments.new], :remote => true) do |f| %>
<%= f.association :comment_title, :collection => #video.comment_titles, :label => "Comment Title:", :include_blank => false %>
<%= f.input :body, :label => false, :placeholder => "Post a comment." %>
<%= f.button :submit, :value => "Post" %>
<% end %>
and this creates a drop down list with this line:
<%= f.association :comment_title, :collection => #video.comment_titles, :label => "Comment Title:", :include_blank => false %>
My question is how do you modify this code so that each drop down item is a link to each comment_title's individual show view?
UPDATE
Here is the generated html from the code from the first answer:
<select class="select optional" id="comment_comment_title_id" name="comment[comment_title_id]">
<option value="<a href=" comment_titles="" 224"="">#<CommentTitle:0x10353b890>">#<CommentTitle:0x10353b890></option>
<option value="<a href=" comment_titles="" 225"="">#<CommentTitle:0x1035296e0>">#<CommentTitle:0x1035296e0></option>
<option value="<a href=" comment_titles="" 226"="">#<CommentTitle:0x1035295a0>">#<CommentTitle:0x1035295a0></option>
</select>
I actually figured it out. Here is the ruby code:
<%= f.association :comment_title, :collection => #video.comment_titles.map {|ct| [ct.title, comment_title_path(ct)] }, :label => "Comment Title:", :include_blank => false %>
This passes the first element in the array as the text, and the second element in the array as the value. Then I use this jQuery code:
$("select").change(function () {
var url = $("select option:selected").val();
$(location).attr("href",url);
});
Pretty simple.
try :collection => #video.comment_titles.map {|ct| [ct, (link_to ct, comment_title_path(ct))] }

Resources