我越來越路線錯誤的URL
::的ActionView ::模板錯誤(沒有路由匹配{:動作=> 「to_approve」:控制器=> 「微柱」:ID => nil}缺少必需的密鑰:[:id]):
沒有路由匹配{:action =>「to_approve」,:controller =>「microposts」, :id => nil}缺少必需的鍵:[:id ]
但它沒有意義,因爲我路由到不同的路線
route.rb
match '/microposts/:id/approve', to: 'microposts#to_approve' , via: [:get, :post], as: 'approve_micropost'
match '/microposts/to_approve', to: 'microposts#approve' , via: :get
controller.rb
def show
@tag = Tag.find(params[:id])
@microposts = @tag.microposts
end
show.html.rb
<%= render @microposts %>
_micropost.html.rb - 這裏是線它顯示的錯誤on
<% if is_an_admin? %>
<%= link_to "Approve", approve_micropost_path(micropost.id) %>
<% end %>
micropost_controller.rb
def approve
@microposts = Micropost.unapproved
end
def to_approve
micropost = Micropost.unapproved_by_id(params[:id])
if micropost.update_attributes(approved: true)
flash[:success] = "Approved!"
else
flash[:error] = "Not approved!"
end
redirect_back_or microposts_to_approve_path
end
micropost.rb
default_scope { where(approved: true).order('microposts.created_at DESC')}
def self.unapproved
self.unscoped.all.where(approved: false).order('microposts.created_at DESC')
end
def self.unapproved_by_id(id = nil)
self.unscoped.all.where(id: id)
end
你可以看到它試圖創建microposts_to_approve_path
與:id
這顯然是不存在的,但我寫approve_micropost_path
。
我錯過了什麼?
此外,在microposts_to_approve_path
路線我允許[:get, :post]
雖然我只希望允許通過on_click
事件訪問to_approve方法(POST?),並沒有看法吧。我應該如何改寫呢?
rake routes
:
microposts POST /microposts(.:format) microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
approve_micropost GET|POST /microposts/:id/approve(.:format) microposts#to_approve
microposts_to_approve GET /microposts/to_approve(.:format) microposts#approve
上的錯誤頁面,參數:
Request
Parameters:
{"id"=>"4",
"name"=>"tag name"}
解決方案
問題是因爲我使用default_scope
,比對象的我與之合作並不行。
修復
@microposts = @tag.microposts #@microposts is CollectionProxy
之前
@microposts = @tag.microposts.all #@microposts is AssociationRelation
一旦我改變.all
問題得到解決之後。
順便說一句,這是一個錯誤?在我的預計default_scope
不應該改變默認行爲。
顯示你'耙routes'導致 – Prashant4224
請出示您的參數。 – thedanotto