call a controller and method on select change with rails - ruby-on-rails

i want to call a AJAX query on select change.
I found this code fragment, but it doesnt work on rails 4, or im doing it wrong
<%= f.select(:resource_id, Resource.all.collect {|resource| [ resource.name, resource.id ] }, :class => "medium", :onchange => remote_function(:url => {:controller => 'controller', :action => 'action'})) %>
and im geting this error
undefined method `remote_function' for #<#<Class:0x007f3daff24a88>:0x007f3d9c670210>
any ideas what im doing wrong ?

This should give you a hint:
<%= f.select(:resource_id, Resource.all.collect {|resource| [ resource.name, resource.id ] }, :class => "medium" %>
#javascript code to make an Ajax call:
$('select#your_model_name_resource_id').on('change', function(event) {
var selected_resource_id = $(this).val();
$.ajax('/relative/path/to/your/action', data: { id: selected_resource_id })
})
When a user change the value of the select, it should perform an Ajax request to your server at this location: /relative/path/to/your/action?id=12 (if id selected was twelve)

The remote_function has been deprecated and removed from rails. You need to write some javascript now (very simple though).
Unless you are willing to install prototype-rails gem, which will add this helper back to your rails.

Related

Ruby on Rails (Rails 2.3.2) f.select onchange() event is not triggered in jQuery

I have creating a select option on using the f.select form helper as follow
<%= f.select(:sales_type, {'Direct' => 'Direct', 'ME' => 'ME', 'Direct Staff' => 'Direct Staff'},{:onchange => "hello();"}) %>
and my JavaScript function is as follow
$(function () {
function helllo(){
alert("from hello");
}
});
while I trigger the f.select I expect the alert box should be shown. But the trigger is not triggered while I choose different options
Let me know which part I did wrong ?
Thanks in advance.
<%= f.select(:sales_type, {'Direct' => 'Direct', 'ME' => 'ME', 'Direct Staff' => 'Direct Staff'},{},{:onchange => "hello();"}) %>
will work i guess. correct syntax for select is
select(method, choices = nil, options = {}, html_options = {}, &block)

Rails - Render partial inside Twitter Bootstrap popover

I want to use a Twitter Bootstrap popover to display a user avatar and email address, with the possibility of adding in more details at a later date.
I am having an issue getting the partial to render inside the content field of the link. The view code is below (in HAML).
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "=render 'users/name_popover'",
class: "comment-user-name"
Currently, this will only produce the content code as a string.
Is there a way to do this so the partial will be inserted instead of the code as a string?
You want rails to actually evaluate the content of the string and not just show the string. The easiest way to do that would be:
%span.comment-username
=link_to comment.user_name, "#", "title" => comment.user_name,
"data-content" => "#{render 'users/name_popover'}",
class: "comment-user-name"
That should pass the render statement to rails and render your partial as expected.
Thanks for the answer jrc - it helped me find a solution.
My solution might be useful for other non HAML people like me:
`<%= link_to('Service History' , '#', :class => "popover-history", :rel => "popover", :"data-placement" => "bottom", :title => "Service History", :"data-content" => "#{render 'services/service_history'}") %>`
with
`$(function () {
$('.popover-history').popover({ html : true });
});`

Make a Select Box that Calls a Method

I have a rails app with working reports that have tags. In the Report/Index.html.erb I want the user to be able to sort the reports by selecting a tag. They may only select one tag at a time so I feel that a select box would work best. I currently have this:
<%= select("preferences", :tag_with,
["Politics", "Technology", "Entertainment", "Sports", "Science", "Crime",
"Business", "Social", "Nature", "Other"], :prompt => "Filter Feed by:" )%>
I have a working preferences controller with a method call tag_with that updates the current tag. This code, however, only generates the select box. I want it to be that when the user selects one of the tags, it calls the tag_with method from the preferences controller.
I generated a series of link_to lines that complete the task, however I would really like a select box.
<%= link_to "Politics", :action => "tag_with", :tag => "Politics", :controller =>"preferences" %>
<%= link_to "Entertainment", :action => "tag_with", :tag => "Entertainment", :controller =>"preferences" %>
<%= link_to "Science", :action => "tag_with", :tag => "Science", :controller =>"preferences" %>
<%= link_to "Technology", :action => "tag_with", :tag => "Technology", :controller =>"preferences" %>
And so on for each tag. This works fine but is bulky and undesirable. Is there a way to do the same thing through a select box?
In your reports.js.coffee file, or whatever other js file you want.
jQuery ->
$('select#preferences').change ->
$.get 'preferences/tag_with',{ term: $('option:selected', this). val() }
Or, if you want to use regular javascript:
$(function(){
$('select#preferences').change( function() {
$.get('preferences/tag_with',{term: $('option:selected',this).val()});
});
});
A link is a GET request. The jQuery .change() method fires whenever someone makes a change. The $.get method sends a GET request to a URL and can pass data (the second argument). This data becomes your params hash, so in the example above you would get:
params[:term] #=> the value attribute of whatever option was selected by the user
See the jQuery docs on .change() and $.get() for more help.
Update
For this to refresh the page, the easiest thing would be to extract the table that you want changed into a partial, let's assume it's called _report.html.erb. The partial should look something like this:
<div id="report">
<%= render #report %>
</div>
*Note: render #report is just short for render :partial => 'report'. See http://guides.rubyonrails.org/layouts_and_rendering.html*
In your preferences controller, tag_with option you should be sure to set the #report object (or whatever else is delivering the data to your partial).
Then you should make a file called views/preferences/tag_with.js.erb and put something like this in it:
$('div#report').html('<%= escape_javascript( render #report ) %>');
This will update the report container with the new content.

How to use a specific controller\action on "onchange" for Select_tag in ruby on rails

Im knew on MVC and on Ruby on rail environment
I have this code
<%= select :language, :language_id,
options_for_select([ "Arabic", "English"]),
{:prompt => "#{t('language')}"},
{:onChange => "#{remote_function(:url => {:controller => 'ConfigurationController',:action => "change_language"}
)}"} %>
And I cant make the Select to call this action and make PostBack for the page on on change
after selected index change nothing is happening ?
Since this is a remote function call. can you see in browser's console if there are any errors returned from server.
the syntax of select_tag, you are using looks fine.
Edit:
did u try alerting some thing onchange event? refer this syntax
<%= select_tag "language", options_from_collection_for_select(#collection,'value','name'), html_options = { :onChange=> "alert('');" :style=> "display:block;" } %>
where u can create your collection using,
#collection = ["en","ab"]
#collection = #collection.map { |name, value| OpenStruct.new(:value => name, :name => name) }
What version of Rails are you using? It looks like remote_function was depracated in 3.1 http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/remote_function
Use jQuery to respond to the change event:
jQuery ->
$("#select_id").change ->
$.ajax(
url: "url",
dataType: "json",
data: "data to send")
.done (data) ->
do_something_on_success()
.fail (data) ->
do_something_on_fail()

Rails 3 - select tag - onchange

I am trying to do select box, where after select some option value will be this value send. Now I have following
<%= select_tag "action", options_for_select([ "1", "2", "3"]) , {},
:onchange =>remote_function(:url => {:action => :index, :id => 0 },
:with => "'action='+ +this.options[this.selectedIndex].value") -%>
and getting error wrong number of arguments (4 for 3). What is there wrong?
EDIT: Can anyone help me please with any function demo or example of form with select, that works with :onchange event (e.g. what should be in controlller)?
I still can't find the right way how to do. :/
Just remove the third argument {}.

Resources