简体   繁体   中英

Specifying a partial to render via Ajax in Rails 3

I'm kind of new to rails, and have recently been trying to implement some ajax. On my user signup page are three buttons to create three different account types.

Here is the link on each:

<%= link_to "sign up", signup_path(:format => 'js'), :remote => true, :rel => 'guest',  :id => 'account-signup-link' %>

In my javascript on the page, when one of these is clicked I add the class 'current' to its anchor tag. Note that I specify the type of account using the rel tag, this varies among the three different links.

My new.js.erb file hit by ajax looks like this...

var template = $('a.current').attr('rel');     
$("<%= escape_javascript render(:file => 'users/_guest.html.erb') %>").insertAfter('#signup-form'); 

How can I replace :file => 'users/_guest.html.erb' with some type of js variable that will switch guest for the rel of the clicked a tag. I have no problem grabbing the appropriate rel, I've tested this with a simple alert in new.js.erb . But for the life of me I can't get a simple string the :file =>... depending on the rel of the clicked button.

Any help would be greatly appreciated.

Thanks, Chris

Trying to access javascript variables is not possible unless you have ruby do the job. Also make sure you white list the params being passed since the user can pass arbitrary data.

Take the data of rel as part of param like signup_path.js?rel=guest and access via params[:rel] . This should be a POST request since its a create url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM