[英]Why does the params “id” key change from params[:id] into params[:model_id]?
When I go to the characters controller, show action, all the normal params[:id] is as how it should be according to REST. 当我转到字符控制器时,请显示操作,所有正常的params [:id]都按照REST的样子。
In the show view, I render a partial. 在显示视图中,我渲染了局部视图。 In that partial, I have a link that goes to the
vote_socionics
action. 在那部分内容中,我有一个链接到前往
vote_socionics
动作。 This action is defined under a socionics_votes
module, which gets included by the characters controller. 此操作在
socionics_votes
模块下定义,该模块已包含在字符控制器中。 (I have it set up this way because I have other controllers that also include this module). (我通过这种方式进行设置,因为我还有其他控制器也包含此模块)。
My problem is that when I click on this link, and it goes to the set_votable
private method within the socionics_votes_module.rb
file, the params[:id]
is no longer present. 我的问题是,当我单击此链接,并转到
socionics_votes_module.rb
文件中的set_votable
私有方法时, params[:id]
不再存在。 Using pry, I found that it actually turns into params[:character_id]
使用撬,我发现它实际上变成了
params[:character_id]
Questions: 问题:
1) Why does this happen (is it because it goes to a "different" controller, even if it's a module?) 1)为什么会发生这种情况(是因为它是一个“不同的”控制器,即使它是一个模块也是如此?)
2) How do I work around this? 2)我该如何解决? I would think that it would be more elegant to have it be params[:id], instead of having to do an if-else to account for both keys.
我认为将其设为params [:id]会更优雅,而不必执行if-else处理这两个键。
characters_controller.rb characters_controller.rb
class CharactersController < ApplicationController
include SocionicsVotesModule
def show
@character = Character.find(params[:id])
end
characters/show.html.haml 字符/ show.html.haml
= render partial: 'votes/vote_socionics',
locals: { votable: @votable, votable_name: @votable_name, socionics: @socionics }
_vote_socionics.html.haml _vote_socionics.html.haml
= link_to content_tag(:div,"a"), send("#{votable_name}_vote_socionics_path", votable, vote_type: "#{s.type_two_im_raw}"),
id: "vote-#{s.type_two_im_raw}",
class: "#{current_user.voted_on?(votable) ? 'voted' : 'not-voted'}",
method: :post,
data: { id: "#{s.type_two_im_raw}" }
socionics_votes_module.rb socionics_votes_module.rb
module SocionicsVotesController
extend ActiveSupport::Concern
included do
before_action :set_votable
end
private
def set_votable
votable_constant = controller_name.singularize.camelize.constantize
@votable = votable_constant.find(params[:id]) # This is where it fails, since there is no params[:id], and rather, params[:character_id]
end
def set_votable_name
@votable_name = controller_name.singularize.downcase
end
routes.rb 的routes.rb
concern :socionics_votes do
post 'vote_socionics'
end
resources :characters, concerns: :socionics_votes
resources :celebrities, concerns: :socionics_votes
resources :users, concerns: :socionics_votes
The URL of the link in the partial when hovered over. 将鼠标悬停在部分链接中的URL。
localhost..../characters/4-cc/vote_socionics?vote_type=neti 本地主机.... /字符/ 4立方厘米/ vote_socionics?vote_type =洗鼻
Something like .find(params[:id] || params[:"#{@votable_name}_id"])
didn't work, and seems silly. 诸如
.find(params[:id] || params[:"#{@votable_name}_id"])
不起作用,而且看起来很愚蠢。
You need to add the vote_socionics
route as a member of the resource: 您需要将
vote_socionics
路由添加为资源的成员:
concern :socionics_votes do
member do
post 'vote_socionics'
end
end
This way the id
parameter gets set correctly 这样,可以正确设置
id
参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.