Rails 4、强参数、嵌套资源、构建和未定义方法许可 [英] Rails 4, strong parameters, nested resources, build and undefined method permit
问题描述
我无法通过构建获得 rails 4,强大的参数来处理嵌套资源.非常欢迎任何建议.
RSPEC 向我展示
创建动作 创建动作失败/错误:click_button创建操作"无方法错误:未定义方法 permit' for "create":String# ./app/controllers/actions_controller.rb:24:in
action_params'# ./app/controllers/actions_controller.rb:10:in create'# ./spec/features/creating_actions_spec.rb:16:in
block (2 levels) in '
我的浏览器显示我:
ActionsController 中的 NoMethodError#createcreate"的未定义方法`permit':字符串
提取的源代码(围绕第 24 行):
定义 action_paramsparams.require(:action).permit(:title, :description)结束
动作控制器包含:
def 创建@action = @project.actions.build(action_params)如果@action.saveflash[:notice] = "操作已创建."redirect_to [@project, @action]别的flash[:alert] = "尚未创建操作."呈现新"结尾结尾私人的def action_paramsparams.require(:action).permit(:title, :description)结尾
我正在使用嵌套资源:
资源:项目做资源:行动结尾
表格如下:
<%= form_for [@project, @action] do |f|%><% if @action.errors.any?%><div id="error_explanation" ><h2><%=pluralize(@action.errors.count, "error") %>禁止保存此操作:</h2><ul><% @action.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><p><%= f.label :title %><br><%= f.text_field :title %></p><p><%= f.label :description %><br><%= f.text_area :描述%></p><%= f.submit %><%结束%>
发生这种情况是因为 Rails 默认发送一个 :action
参数,其中包含请求的操作.当您执行 params.require(:action)
时,您实际上将请求的操作名称作为字符串返回,在本例中为 create.您可以明确告诉 form_for
使用另一个密钥作为哈希,解决您的强参数问题:
<%= form_for [@project, @action], 如: :foo do |f|%>
在你的控制器中:
params.require(:foo).permit...
I am unable to get rails 4, strong parameters to work with nested resources via build. Any suggestions would be very welcome.
RSPEC shows me
Creating Actions Creating an action
Failure/Error: click_button "Create Action"
NoMethodError:
undefined method permit' for "create":String
# ./app/controllers/actions_controller.rb:24:in
action_params'
# ./app/controllers/actions_controller.rb:10:in create'
# ./spec/features/creating_actions_spec.rb:16:in
block (2 levels) in '
My Browser shows me:
NoMethodError in ActionsController#create undefined method `permit' for "create":String
Extracted source (around line #24):
def action_params params.require(:action).permit(:title, :description) end
Actions Controller contains:
def create
@action = @project.actions.build(action_params)
if @action.save
flash[:notice] = "Action has been created."
redirect_to [@project, @action]
else
flash[:alert] = "Action has not been created."
render "new"
end
end
private
def action_params
params.require(:action).permit(:title, :description)
end
I am using nested resources:
resources :projects do
resources :actions
end
The form is as following:
<%= form_for [@project, @action] do |f| %>
<% if @action.errors.any? %>
<div id="error_explanation" >
<h2><%= pluralize(@action.errors.count, "error") %>
prohibited this action from being saved:</h2>
<ul>
<% @action.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<%= f.submit %>
<% end %>
This is happening because Rails by default send a :action
param containing the action that was requested. When you do params.require(:action)
you're actually returning the name of the action requested as a string, in this case, create. You can explicitly tell form_for
to use another key for the hash, solving your strong parameter problem:
<%= form_for [@project, @action], as: :foo do |f| %>
And in your controller:
params.require(:foo).permit...
这篇关于Rails 4、强参数、嵌套资源、构建和未定义方法许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!