Reputation: 658
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
Upvotes: 0
Views: 379
Reputation: 2898
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.
Upvotes: 1