Ruby on rails 4 ActiveModel::禁止BooksController中的属性错误#创建

Ruby on rails 4 ActiveModel::禁止BooksController中的属性错误#创建,ruby-on-rails-4,Ruby On Rails 4,当我使用/books/new访问我的new.erb文件时,我的应用程序名为LibraryWebApplication,它给出了表格,然后我用标题、价格、,描述然后我单击按钮创建它将转到以下url/books/create,但它没有将数据存储到数据库中,显示以下错误 ActiveModel::禁止BooksController中的属性错误#创建 ActiveModel::禁止属性错误 我是rails新手,无法找到解决方案 提取的源(第13行附近): Rails.root:D:/railsapsex

当我使用/books/new访问我的new.erb文件时,我的应用程序名为LibraryWebApplication,它给出了表格,然后我用标题、价格、,描述然后我单击按钮创建它将转到以下url/books/create,但它没有将数据存储到数据库中,显示以下错误 ActiveModel::禁止BooksController中的属性错误#创建 ActiveModel::禁止属性错误 我是rails新手,无法找到解决方案 提取的源(第13行附近):

Rails.root:D:/railsapsexamples/libraywebapplication

stacktrace是

    Started POST "/books/create" for 127.0.0.1 at 2014-03-18 14:58:25 +0530
    Processing by BooksController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"TfBODzvGFAE6RkCmfPAAx/EhkSJkCeYUemr129dKYjc=", "book"=>{"title"=>"Advanced Physics", "price"=>"523", "subject_id"=>"2", "description"=>"mathsbk"}, "commit"=>"Create"}
    Completed 500 Internal Server Error in 0ms

    ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
      app/controllers/books_controller.rb:13:in `create'


      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (15.6ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (15.6ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (62.5ms)
    [2014-03-18 15:03:14] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


    Started POST "/books/create" for 127.0.0.1 at 2014-03-18 15:04:48 +0530
    Processing by BooksController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"TfBODzvGFAE6RkCmfPAAx/EhkSJkCeYUemr129dKYjc=", "book"=>{"title"=>"Advanced Physics", "price"=>"523", "subject_id"=>"2", "description"=>"mathsbk"}, "commit"=>"Create"}
    Completed 500 Internal Server Error in 0ms

    ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
      app/controllers/books_controller.rb:13:in `create'


      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (31.2ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
      Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (78.1ms)
我有两种型号

book.rb

    class Book < ActiveRecord::Base
        belongs_to :subject
        validates_presence_of :title
      validates_numericality_of :price, :message=>"Error Message"
    end
新的.erb文件是

    <h1>Add new book</h1>
    <%= form_tag :action => 'create' %>
    <p><label for="book_title">Title</label>:
    <%= text_field 'book', 'title' %></p>
    <p><label for="book_price">Price</label>:
    <%= text_field 'book', 'price' %></p>
    <p><label for="book_subject">Subject</label>:
    <%= collection_select(:book,:subject_id,@subjects,:id,:name) %></p>
    <p><label for="book_description">Description</label><br/>
    <%= text_area 'book', 'description' %></p>
    <%= submit_tag "Create" %>
    <%= link_to 'Back', {:action => 'list'} %>
添加新书
“创建“%”
标题:

价格:

主题:

说明

'列表'}%>
迁移文件是

20140318084539_books.rb

    class Books < ActiveRecord::Migration
     def self.up
         create_table :books do |t|
      t.column :title, :string, :limit => 32, :null => false
      t.column :price, :float
      t.column :subject_id, :integer
      t.column :description, :text
      t.column :created_at, :timestamp
         end
      end

      def self.down
        drop_table :books
      end
    end
classbooks32,:null=>false
t、 列:价格,:浮动
t、 列:subject_id,:integer
t、 列:说明,:文本
t、 列:创建时间::时间戳
终止
终止
def自动关闭
放下桌子:书
终止
终止
20140318084609_subjects.rb

    class Subjects < ActiveRecord::Migration
      def self.up
          create_table :subjects do |t|
           t.column :name, :string
        end
        Subject.create :name => "Physics"
        Subject.create :name => "Mathematics"
        Subject.create :name => "Chemistry"
        Subject.create :name => "Psychology"
        Subject.create :name => "Geography"
      end

      def self.down
          drop_table :subjects
      end
    end
类主题“物理”
Subject.create:name=>“数学”
Subject.create:name=>“化学”
Subject.create:name=>“心理学”
Subject.create:name=>“地理”
终止
def自动关闭
下表:受试者
终止
终止
上面的代码是在数据库中的表中创建的,必须在database.yml.中编写测试和生产。我们可以只写开发吗


我正在使用带有eclipse插件的Rails4.0.3,我真的建议您阅读RailsGuide,因为您的代码存在一些严重的问题。当我坚持你的问题时,答案是你必须使用强参数。这意味着什么?好的,强参数决定哪些属性可以被列入白名单

在Rails 4之前,通过attr_accessible在模型中列出了白名单属性。Rails 4使用属于控制器的强参数

我将给出一个基本示例:

  def create
    @book = Book.new(book_params)

    <rest of code>
  end

private
  def book_params
    params.require(:book).permit(:title, :content)
  end
def创建
@book=book.new(book\u参数)
终止
私有的
def book_参数
参数要求(:书籍)。许可证(:标题,:内容)
终止
现在不再禁止属性title和content,因为您明确允许使用强参数


我想我应该写一篇针对强参数的文章,因为关于它们似乎有一些混淆。在这里,您可以看到一个具有强大参数的示例控制器:

我真的建议您阅读RailsGuide,因为您的代码存在一些严重的问题。当我坚持你的问题时,答案是你必须使用强参数。这意味着什么?好的,强参数决定哪些属性可以被列入白名单

在Rails 4之前,通过attr_accessible在模型中列出了白名单属性。Rails 4使用属于控制器的强参数

我将给出一个基本示例:

  def create
    @book = Book.new(book_params)

    <rest of code>
  end

private
  def book_params
    params.require(:book).permit(:title, :content)
  end
def创建
@book=book.new(book\u参数)
终止
私有的
def book_参数
参数要求(:书籍)。许可证(:标题,:内容)
终止
现在不再禁止属性title和content,因为您明确允许使用强参数


我想我应该写一篇针对强参数的文章,因为关于它们似乎有一些混淆。在这里,您可以看到一个具有强大参数的示例控制器:

这个问题经常出现。签出右侧的链接,它们会将您指向正确的方向(提示:强参数)。质量分配在强参数中是什么意思?方法应与控制器的方法名称相关?来自:带有强参数,动作控制器参数在被列入白名单之前,禁止在活动模型质量分配中使用。我无法编写强参数,这表明我对ruby和Rails是新手。这个问题在这方面涉及很多。签出右侧的链接,它们会将您指向正确的方向(提示:强参数)。质量分配在强参数中是什么意思?方法应与控制器的方法名称相关?来自:带有强参数,在活动模型质量分配中禁止使用动作控制器参数,除非这些参数已被列入白名单。我无法编写强参数,因为它显示了我对ruby和railsok不熟悉的错误,我将接受。params.require(:book).permit(:title,:content)在此标题和内容中是表单的参数?是的,这只是一个示例。在这种情况下,只有书名和内容可以访问。好的,我接受。params.require(:book).permit(:title,:content)在此标题和内容中是表单的参数?是的,这只是一个示例。在这种情况下,只能访问书籍的标题和内容。
    class Subjects < ActiveRecord::Migration
      def self.up
          create_table :subjects do |t|
           t.column :name, :string
        end
        Subject.create :name => "Physics"
        Subject.create :name => "Mathematics"
        Subject.create :name => "Chemistry"
        Subject.create :name => "Psychology"
        Subject.create :name => "Geography"
      end

      def self.down
          drop_table :subjects
      end
    end
  def create
    @book = Book.new(book_params)

    <rest of code>
  end

private
  def book_params
    params.require(:book).permit(:title, :content)
  end