2015-12-23 53 views
2

我試圖通過單擊Link_to呈現boxes_list。不知道爲什麼它不工作。Link_to不從自定義控制器動作呈現部分

# Routes.rb  
resources :modifications do 
    collection do 
     get 'refresh' 
    end 
end 


# ModificationsController 
    def refresh 
    respond_to do |format| 
     format.js {} 
    end 
    end 


# link in /views/modifications/_boxes_list.html.erb that should refresh boxes_list 
<%= link_to "refresh", refresh_modifications_path(@modification), remote: true, method: :refresh %> 


# JS responce in /views/modifications/refresh.js.erb 
$('#boxes_count').html("<%= escape_javascript(render(:partial => 'boxes_list')).html_safe %>"); 

在服務器控制檯中,當按此鏈接時,我什麼都看不到。鏈接在正常顯示操作下的修改顯示頁面上。 Rails 4!

回答

0

爲什麼你把method: :refresh。從鏈接中刪除method: :refresh。你route應該

resources :modifications do 
    member do 
    get :refresh 
    end 
end 

那麼你的路徑應該是

<%= link_to "refresh", refresh_modification_path(@modification), remote: true %> 

而且在'刷新」行動

def referesh 
    @modification = Modification.find(params[:id]) 
    respond_to do |format| 
    format.js{} 
    end 
end 

來源Adding More RESTful Actions

+0

我嘗試了,但現在我有這樣的:'已完成406不可接受在3ms ActionController :: UnknownFormat(ActionController :: UnknownFormat):app/controllers/modifications_controller.rb:18:in'refresh'' – DanielsV

+0

您可以從'/ views/modifications/refresh.js.erb'文件中發表評論,並寫下'alert(Hello)'。這會測試你的代碼,它會通過'ajax'調用進行'刷新'。試過讓我知道 –

+0

,但是與未知格式相同的問題。警報不要射擊! – DanielsV

2

您首先應該從link_to刪除method: :refresh (你不需要它):

<%= link_to "refresh", refresh_modifications_path, remote: true %> 

如果您使用collection路由,您也不需要提供對象。如果您使用member路線,則必須傳遞該對象。

-

爲了節省試圖通過代碼來挑選的麻煩,這裏是你應該擁有的一切:

#config/routes.rb 
resources :modifications do 
    get :refresh, on: :member #-> url.com/modifications/:id/refresh 
end 

#app/controllers/modifications_controller.rb 
class ModificationsController < ApplicationController 
    respond_to :js, only: :refresh 
    def refresh 
    end 
end 

#app/views/modifications/refresh.js.erb 
$('#boxes_count').html("<%=j render partial: 'boxes_list' %>"); 

你會按如下發出請求:

<%= link_to "Refresh", refresh_modification_path(@modification), remote: true %> 
+0

在'member'路由的情況下,link_to應該是'refresh_modification_path(@modification)'(不包括s) –

+0

謝謝 - 更新 –

+0

現在我得到這個:NoMethodError(未定義的方法'盒'爲零:NilClass):不能訪問部分關聯。 – DanielsV