Ruby on rails 铁路及;强参数';缺少必需的参数';
我正在为我的一个项目创建“转发”(restream)功能,但我一直遇到以下错误:Ruby on rails 铁路及;强参数';缺少必需的参数';,ruby-on-rails,parameters,strong-parameters,Ruby On Rails,Parameters,Strong Parameters,我正在为我的一个项目创建“转发”(restream)功能,但我一直遇到以下错误: Required parameter missing: restream 我不确定我错过了什么。这是我的设置。有什么想法吗 型号 #app/models/member.rb Class Member < ActiveRecord::Base has_many :statuses end #app/models/status.rb Class Status< ActiveRecord::Bas
Required parameter missing: restream
我不确定我错过了什么。这是我的设置。有什么想法吗
型号
#app/models/member.rb
Class Member < ActiveRecord::Base
has_many :statuses
end
#app/models/status.rb
Class Status< ActiveRecord::Base
belongs_to :member
has_many :restreams, class_name: "Status", foreign_key: "restream_id"
end
#app/models/member.rb
类成员
重排
#config/routes.rb
resources :statuses do
member do
post :retweet
end
end
#app/controllers/statuses_controller.rb
def restream
@restream = Status.new(restream_params)
@restream.save
end
private
def restream_params
params.require(:restream).permit(:restream_id, :content).merge(member_id: current_user.id)
end
#app/views/statuses/show.html.erb
<%= link_to image_tag("Re-Stream 3.png", class: "act_actions", title: "Restream", alt: "Restream"), restream_status_path(status.id), method: :post, :class => "btn restream" %>
#config/routes.rb
资源:状态是什么
成员do
帖子:转发
终止
终止
#应用程序/控制器/状态\u controller.rb
def重蒸汽
@restream=状态。新建(restream_参数)
@重排
终止
私有的
def restream_参数
参数require(:restream).permit(:restream_id,:content).merge(成员id:current_user.id)
终止
#app/views/status/show.html.erb
“btn重组”%>
您需要通过重新整理参数
。您还需要在get中发送它,并指定restream\u参数的值,因为它是指向的链接。
#app/views/statuses/show.html.erb
<%= link_to image_tag("Re-Stream 3.png", class: "act_actions", title: "Restream", alt: "Restream"), restream_status_path(status.id), params.merge({:restream => "the value you want to pass"}) method: :get, :class => "btn restream" %>
#app/views/status/show.html.erb
“要传递的值”})method::get,:class=>“btn restream”%>
您的错误可以通过查看以下内容得到最好的解释:
示例
{“公司”:{“名称”:“acme”,“地址”:“胡萝卜街123号”}
您将获得参数[:company]作为{“name”=>“acme”,“address”=>“123
胡萝卜街“}
strong参数
#app/models/member.rb
Class Member < ActiveRecord::Base
has_many :statuses
end
#app/models/status.rb
Class Status< ActiveRecord::Base
belongs_to :member
has_many :restreams, class_name: "Status", foreign_key: "restream_id"
end
要使您的strong_params
正常工作,您的params散列需要如下所示:
{ "restream": {"your":"params", "go":"here"} }
{"your":"param", "goes":"here"}
问题是您的params散列将如下所示:
{ "restream": {"your":"params", "go":"here"} }
{"your":"param", "goes":"here"}
修复
有两种方法可以解决此问题:
def restream_params
params.permit(:restream_id, :content).merge(member_id: current_user.id)
end
这将允许:restream_id
和:content params
而无需将它们嵌入params散列的:restream
选项中
另一个选项是通过链接传递:restream
参数。Sidney的回答很好,但我会使用,因为我知道这会根据需要设置参数散列:
<%= form_for @restream %>
<%= text_field :restream_id %>
<% end %>
这里的错误信息非常清楚。您没有将必需的restream
参数传递到restream
操作中。好的,我知道我缺少什么,所以我将代码更改为:“btn restream”%>
我想我只是不确定应该传递什么对象来使事情正常运行