Ruby on Rails 4 Essential Training Error ActionController :: ParameterMissing in SubjectsController #create

时间:2014-11-11 23:11:35

标签: ruby-on-rails

我正在使用Lynda.com的Ruby on Rails 4 Essential Training,我遇到了这个错误:

Actionstroller :: SubjectMissing在SubjectsController #create

我基本上有一个带有3个输入的表单:name,:position和:visible。我可以输入信息就好但是当我点击提交按钮时我收到错误。以下是服务器的输出:

   `Started POST "/subjects/create" for 127.0.0.1 at 2014-11-11 18:02:12 -0500
Processing by SubjectsController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"f6+AdWN3jWO6mL9jrPDgVGoAm/NTBF1GPxGasTaqMh0=", "subject"=>{"name"
=>"Default", "position"=>"5", "visible"=>"false"}, "commit"=>"Create Subject"}
   (0.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `subjects` (`created_at`, `name`, `position`, `updated_at`) VALUES ('2014-11-11 23:02:12'
, 'Default', 5, '2014-11-11 23:02:12')
   (4931.3ms)  COMMIT
Redirected to http://localhost:3000/subjects/create?actions=index
Completed 302 Found in 4941ms (ActiveRecord: 4932.3ms)


Started GET "/subjects/create?actions=index" for 127.0.0.1 at 2014-11-11 18:02:17 -0500
Processing by SubjectsController#create as HTML
  Parameters: {"actions"=>"index"}
Completed 400 Bad Request in 1ms

ActionController::ParameterMissing (param is missing or the value is empty: subject):
  app/controllers/subjects_controller.rb:40:in `subject_params'
  app/controllers/subjects_controller.rb:18:in `create'

SubjectsController

    class SubjectsController < ApplicationController

  layout false

  def index
    @subjects = Subject.sorted
  end

  def show
    @subject = Subject.find(params[:id])
  end

  def new
    @subject = Subject.new({:name => "Default"})
  end

  def create
    @subject = Subject.new(subject_params)
    if @subject.save

      redirect_to(:actions => 'index')
      # raise params.inspect
    else
      render('new')
    end
  end


  def edit
  end

  def delete
  end

  private
    def subject_params
      # same as using "params[:subject]", except that it:
      # - raises an error if :subject is not present
      # - allows listed attributes to be mass-assigned
      params.require(:subject).permit(:name, :position, :visible)
    end


end

查看服务器输出,即使错误发生后,即使数据显示在索引页面上,我也不会将:visible属性传递给数据库。

1 个答案:

答案 0 :(得分:2)

我不认为:visible是问题所在,查看您的日志,您收到此错误:

ActionController::ParameterMissing (param is missing or the value is empty: subject):
  app/controllers/subjects_controller.rb:40:in `subject_params'
  app/controllers/subjects_controller.rb:18:in `create'

这是因为这一行:

Started GET "/subjects/create?actions=index" for 127.0.0.1 at 2014-11-11 18:02:17 -0500
Processing by SubjectsController#create as HTML
  Parameters: {"actions"=>"index"}

您似乎正在重定向到GET /subjects/create并传入参数actions=index,这可能是由您的控制器中的此行引起的:

redirect_to(:actions => 'index')

我认为你的意思是:action(单数):

redirect_to(:action => 'index')

关于:visible没有被保存的问题,我不能在不了解模型的情况下说太多,在数据库模式中声明了visible吗?