2013-01-07 58 views
0

我正在使用link_to渲染局部(如圖所示,在單擊link_to時渲染局部),如圖所示。我想通過參數u.id到部分,但我沒有成功:(我做錯了什麼?我絕望。在link_to和渲染局部之間傳遞參數

編輯:終於得到它的工作。這是我的代碼:

在我看來,用戶/ show.html.erb - 「U」 是一個集合

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

在每個工具在我的用戶#結束顯示控制器方法

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%>'); 

我的部分:_utilEdit.html.erb

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

謝謝大家誰幫助!

回答

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

link_to:util_id這種格式去html_options,所以你得到的HTML類似

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

但是,你需要像

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

嘗試使用命名的道路,或者{ :controller, :action, :params }結構而不是

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

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

謝謝!但我得到錯誤「未定義的方法'show_users_path'爲#<#:0x007fb79b36aa88>」:(: – user1436111

+0

修復它,它現在可以工作! – user1436111