首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails 4基本训练错误ActionController::ParameterMissing in SubjectsController#create

Ruby on Rails 4基本训练错误ActionController::ParameterMissing in SubjectsController#create
EN

Stack Overflow用户
提问于 2014-11-12 07:11:35
回答 1查看 127关注 0票数 0

我正在参加来自Lynda.com的Ruby on Rails 4基本培训,并在这个错误中运行:

SubjectsController#create中缺少ActionController::参数

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

代码语言:javascript
复制
   `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'

代码语言:javascript
复制
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属性传递给数据库,即使数据显示在索引页上,即使在出错之后也是如此。

EN

回答 1

Stack Overflow用户

发布于 2014-11-12 07:32:27

我不认为:visible是问题所在,看看你的日志,你会得到这个错误:

代码语言:javascript
复制
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'

这是因为这一行:

代码语言:javascript
复制
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,这可能是由你的控制器中的这一行引起的:

代码语言:javascript
复制
redirect_to(:actions => 'index')

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

代码语言:javascript
复制
redirect_to(:action => 'index')

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26876364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档