Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

ruby on rails - ActionController::ParameterMissing (param is missing or the value is empty: film):

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

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

<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

<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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:.

I fix it by adding params.fetch(:film, {}) the strong parameter into:

params.fetch(:film, {}).permit(:name, :sort_name, :description, :short_description, :meta_data,
    :poster, :amazon_link, :active, :trackable, :country_ids => [])

I refer to code example to avoid ActionController::ParameterMissing (param is missing or the value is empty: film)

I hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...