user1436111
user1436111

Reputation: 2141

Passing params between link_to and render partial

I'm using link_to to render a partial (as in, the partial is rendered when the link_to is clicked), as shown. I want to pass the param u.id into the partial but I'm having no success :( What am I doing wrong? I'm desperate.

Edit: Finally got it to work. Here is my code:

In my view, users/show.html.erb - "u" is each Utility in a collection

<% @utilities.each do |u| %>
    <%= link_to u.name, { :controller => :users, :action => :show, :util_id => u.id }, :remote => true %>
<% end %>

At the end of my users#show controller method

    respond_to do |format|
        format.html {
            redirect_to @current_user and return
        }
        format.json {
            redirect_to @current_user.json and return
        }
        format.js {
          @util_id = params[:util_id]
          redirect_to @current_user and return
        }
    end

Show.js.erb:

$("#editUtil").prepend('<%= escape_javascript(raw render :partial => "utilEdit", :locals => {:util => params[:util_id]} ).html_safe%>');

My partial: _utilEdit.html.erb

TEST: <%= Utility.find(util).name %>

Thank you to everyone who helped!!

Upvotes: 0

Views: 1405

Answers (1)

dimuch
dimuch

Reputation: 12818

<%= link_to u.name, '/users/show', :util_id => u.id, :remote => true %>

In this format of link_to :util_id goes to html_options, so you get html like

<a href="/users/show" util_id="1" data-remote="true">...

But you need something like

<a href="/users/show?util_id=1" data-remote="true">...

Try to use named path, or { :controller, :action, :params } constructions instead

<%= link_to u.name, show_users_path(:util_id => u.id), :remote => true %>

or

<%= link_to u.name, { :controller => :users, :action => :show, :util_id => u.id }, :remote => true %>

Upvotes: 0

Related Questions