Ruby-on-rails ActionController::ParameterMissing(缺少参数或值为空:电影):
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24944871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionController::ParameterMissing (param is missing or the value is empty: film):
提问by user3399101
So, I'm getting the following error when trying to visit the films page on my app:
因此,当我尝试访问我的应用程序上的电影页面时出现以下错误:
ActionController::ParameterMissing (param is missing or the value is empty: film):
2014-07-24T22:04:44.622356+00:00 app[web.1]: app/controllers/saas_admin/films_controller.rb:54:in `permitted_params'
See my films controller code below
请参阅下面的我的电影控制器代码
films_controller.rb
电影控制器.rb
class SaasAdmin::FilmsController < SaasAdminController
inherit_resources
belongs_to :studio, :finder => :find_by_id!, :param => :studio_id, :class_name => Studio
before_filter :set_sort_fields, :only => :edit
before_filter :build_collections, :only => [:new, :create, :edit, :update]
def create
create! { parent_path(parent.id) } # Redirect to studio in case studio_id is changed
end
def update
@film = Film.find_by_permalink(params[:id])
respond_to do |format|
if @film.update(permitted_params)
format.html { redirect_to saas_admin_studio_path(@film.studio), notice: 'Film was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @film.errors, status: :unprocessable_entity }
end
end
end
def index
redirect_to parent_path(parent.id)
end
def show
@clips = resource.clips.paginate(:page => params[:page], :per_page => 30, :order => 'clips.position')
end
protected
def resource
# @film ||= end_of_association_chain.find_by_permalink!(params[:id])
@film ||= end_of_association_chain.find_by_permalink!(params[:id])
end
def collection
@films ||= end_of_association_chain.paginate(:page => params[:page], :per_page => 30, :order => 'films.created_at')
end
def set_sort_fields
resource.sort_name = '' if resource.name == resource.sort_name
end
def build_collections
@studios ||= Studio.find(:all)
end
def permitted_params
params.require(:film).permit(:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable, :country_ids => [])
end
end
What might this be? I've been trying to figure it out for a bit but perhaps a fresh set of eyes will find it's something rather simple.
这可能是什么?我一直试图弄清楚它,但也许一组新的眼睛会发现它相当简单。
Cheers!
干杯!
Edit
编辑
Here's the view code for films/new.html.erb
这是films/new.html.erb的查看代码
<h1><%= @page_title = "New #{resource_class}" %></h1>
<%= form_for resource, :url => collection_path, :html => { :multipart => true } do |f| -%>
<%= render :partial => "form", :locals => { :f => f } %>
<% end -%>
<% content_for :sidebar do %>
<%= render :partial => "saas_admin/shared/sidebar" %>
<% end %>
and films/edit.html.erb
和电影/edit.html.erb
<h1><%= @page_title = "Edit #{resource_class}" %></h1>
<%= form_for resource, :url => saas_admin_studio_film_path(parent, resource), :html => { :multipart => true } do |f| -%>
<%= render :partial => "form", :locals => { :f => f } %>
<% end -%>
<% content_for :sidebar do %>
<%= render :partial => "saas_admin/shared/sidebar" %>
<% end %>
Edit 2
编辑 2
For reference here is how the permitted params was defined when it was working:
作为参考,这里是如何定义允许的参数在工作时的:
def permitted_params
{:film => params.fetch(:film, {}).permit(
:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable)}
end
采纳答案by derekyau
This is happening because you have specified to require 'film' in your parameters through strong_params (specified above in your permitted_params code).
发生这种情况是因为您已通过 strong_params(在上面的 permited_params 代码中指定)指定在您的参数中使用“film”。
Whatever the view side is doing (whether its a link or a form/etc.), its not passing its parameters nested under 'film'
无论视图端在做什么(无论是链接还是表单/等),它都不会传递嵌套在“电影”下的参数
eg.) if you were to raise params.inspectin the controller action, you would see that there is no node for "film".
例如。)如果您要raise params.inspect在控制器操作中,您会看到没有“电影”节点。
Most likely what is wrong is that the form code you have on the view side is not set to nest these parameters properly, are you using a form_tagfor example?
最有可能的错误是您在视图端的表单代码未设置为正确嵌套这些参数,form_tag例如您是否使用了?
回答by akbarbin
I have got this problem too when I use Angular JS form to send data to backend Rails 4. When I did not fill anything in angular js form, the error will show ActionController::ParameterMissing (param is missing or the value is empty:.
当我使用Angular JS表单向后端Rails 4发送数据时,我也遇到了这个问题。当我没有在angular js表单中填写任何内容时,错误会显示ActionController::ParameterMissing (param is missing or the value is empty:。
I fix it by adding params.fetch(:film, {})the strong parameter into:
我通过将params.fetch(:film, {})强参数添加到以下内容来修复它:
params.fetch(:film, {}).permit(:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable, :country_ids => [])
我参考代码示例来避免 ActionController::ParameterMissing (param is missing or the value is empty: film)
I hope this will help you.
我希望这能帮到您。
回答by Alex cueto
Why not use so:
为什么不这样使用:
def creation_params
params.permit(:film)
end
It working for me! ;)
它对我有用!;)

