I want to pass collection_select drop down list value with link_to_remote.
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
<%= link_to_remote 'Show calendar', :url => {:controller => 'calendar', :action => 'trainer_view'} %>
I want to pass the selected trainer_id value to the trainer_view method. How can I do this?
Hi :) I would recommend using jQuery and AJAX to accomplish your goal, this is how I would try to do it:
First, I would drop the <%= link_to_remote %> under the collection_select so it would look like this:
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => 'Select a Trainer'}) %>
<div id="trainerCalendar"></div>
Then, put this JavaScript in your application.js, when the DOM is ready:
$('#trainer_id').live('change', function() {
$('#trainerCalendar').html.empty;
$.ajax({ url: '/trainer_view/',
data: 'id=' + this.value,
success: function(data) {
$('#trainerCalendar').html(data);
}
})
});
You can have your controller respond with a partial of the calendar.
Hope this helps!
Related
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 %>
In my application, one article can have multiple tags. When creating an article, I am able to add multiple tags to an article using a multiple-select dropdown and the Rails Select2 gem.
The problem is that when I edit the article, none of the tags are selected, and a user has to select them all over again.
<%= form_for(#article) do |f| %>
<%= f.select :tags, options_for_select(#tags.collect {|t| [ t.id, t.title] }),{:selected=>#article.tags}, :multiple => true, :id => "article_tags", :class => "form-control" %><br>
<%= f.submit 'Update', :class => "btn" %><br>
<% end %>
<script>
$(document).ready(function() {
$("#article_tags").select2({multiple:true});
});
</script>
Any suggestions?
This ended up working when I put it at the bottom of the page:
<script>
$(document).ready(function() {
$("#tags").select2({multiple:true});
$('select').select2().select2('val', 'tag one, tag two, tag three');
$('select').select2();
});
</script>
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" } %>
I have the following select list.
<%= select_tag "project_id", options_from_collection_for_select(#projects, "id", "title") %>
When the user selects an option from the above list, the list below should be populated with values from database based on the above selection.
<%= select_tag "version_id", options_from_collection_for_select(??project.versions??, "id", "title") %>
I think i should make use of the onchange event, but i have no idea on how to use it.
Someone help me please.
Thanks!
Javascript
function update_versions_div(project_id) {
jQuery.ajax({
url: "/update_versions",
type: "GET",
data: {"project_id" : project_id},
dataType: "html"
success: function(data) {
jQuery("#versionsDiv").html(data);
}
});
}
Controller
def edit
#projects = Project.all
#versions = Version.all
end
def update_versions
#versions = Version.where(project_id => params[:project_id]).all
render :partial => "versions", :object => #versions
end
View
<%= select_tag "project_id", options_from_collection_for_select(#projects, "id", "title"), :prompt => "Select a project", :onchange => "update_versions_div(this.value)" %>
<div id="versionsDiv">
<%= render :partial => 'versions', :object => #versions %>
</div>
Partial: _version.html.erb
<%= select_tag "version_id", options_from_collection_for_select(versions, "id", "title"), :prompt => "Select a version" %>
Also add a route for /update_versions in your routes.rb
match "/update_versions" => "<controller>#update_versions"
Here, you should replace <controller> with name of the controller.
I havent tested the code, so there may be errors.
Update
PullMonkey has updated the code with Rails 3 example, which is obviously superior than this code. Please checkout http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/ also
I'm using
<%= text_field 'person', 'one',:id => 'test', :onchange=>remote_function(:url=>{:action=>"update"}, :update=>"test") %>
<div id="test"></div>
Now I just want to send the value of text_field with :action
i.e :url=>{:action=>"update(value_of_text_field_entered"}
Don't want to use params[:person][:one].
Any suggestions?or how can I use <%= observe_field %> to send the value with action?
You can try this one also:
<%= text_field 'person', 'one' %>
### Paste following code in your javascript
$("#person_one").live("change", function(){
$.ajax({
complete:function(request){},
data:'person_one='+ $(this).val(),
dataType:'script',
type:'get',
url: '/update'route
})
});
maybe you can use a javascript code, "this.value()"
Use
<%= text_field 'person', 'one',:id => 'test' %>
<%= observe_field 'persone_one',
:url=>{:action=>"update"} ,
:frequency => 0.25,
:update=>'test',
:with => 'person_one'
%>
<div id="test"></div>
you get value of textfiled in params[:person_one]