Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an onclick option to link_to method in rails?

I want to add an onclick option to link_to method for loading an modal dialog box...i am using rails version 2.3.8 and i searched on google and could not do it. Plz anybody help me?

My link_to method as follows.

<%= link_to 'All countries',{:controller=>'countries', :action=>'new'}, :remote => true %>
like image 214
Rosh Avatar asked Sep 18 '11 06:09

Rosh


1 Answers

If you are using 2.3.8, you don't have :remote => true. You need to use link_to_remote if you are try to do an ajax action.

So it would look something like:

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}%>
<div id="populate_me"></div>

and your new method would have to handle the ajax request with something like

countries_controller.rb

def new
  <do something>
  render :update do |page|
    page.replace_html 'populate_me', :partial => 'whatever'
  end
end

UPDATED

If you want the onclick in addition to the ajax action, you can just pass it into the html options:

<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}, :html => {:onclick => 'alert("some javascript executed before ajax")'} %>
like image 181
Chris Barretto Avatar answered Oct 23 '22 19:10

Chris Barretto