Ruby on rails 4 ActiveModel::禁止BooksController中的属性错误#创建 我对ruby和rails都是新手,现在我正在使用Rails4和Eclipse IDE开发一个应用程序。该应用程序是库应用程序。功能主义者是新的,创建书籍、显示、列出、删除和编辑书籍等。 我在浏览器中输入http://127.0.0.1:3000/books/new 当我点击按钮时,它显示了一个带有标题、价格、主题和描述的表单,并显示了下面的错误 ActiveModel::禁止BooksController中的属性错误#创建 ActiveModel::禁止属性错误 def创建 @book=book.new(params[:book])——这是第13行 如果@book.save 重定向到:action=>“list” ActiveModel::ForbiddenAttribute错误(ActiveModel::ForbiddenAttribute错误): app/controllers/books\u controller.rb:13:in'create' 呈现c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action\u dispatch/middleware/templates/rescues//u source.erb(0.0ms)。。。。。 它有两种型号 book.rb 类目“错误消息”的数值性 结束 subject.rb 类主题“新建” 结束 结束 私有的 def book_参数 参数要求(:书)。许可(:标题,:价格,:主题,:描述) 结束 定义编辑 @book=book.find(参数[:id]) @主题=主题。查找(:全部) 结束 def更新 @book=book.find(参数[:id]) 如果@book.update_属性(参数[:book]) 重定向到:action=>'show',:id=>@book 其他的 @主题=主题。查找(:全部) 呈现:操作=>“编辑” 结束 结束 def删除 Book.find(params[:id]).destroy 重定向到:action=>“list” 结束 def show_主题 @subject=subject.find(参数[:id]) 结束 结束 路由文件是 routes.rb LibrayWebApplication::Application.routes.draw do 获取“书籍/新” 发布“书籍/创建” 获取“书籍/列表” 获取“书籍/展览” 获取“书籍/编辑” 获取“书籍/展示主题” 结束 并且视图文件是 new.erb 添加新书 “创建“%”
标题: 价格: 主题: 说明Ruby on rails 4 ActiveModel::禁止BooksController中的属性错误#创建 我对ruby和rails都是新手,现在我正在使用Rails4和Eclipse IDE开发一个应用程序。该应用程序是库应用程序。功能主义者是新的,创建书籍、显示、列出、删除和编辑书籍等。 我在浏览器中输入http://127.0.0.1:3000/books/new 当我点击按钮时,它显示了一个带有标题、价格、主题和描述的表单,并显示了下面的错误 ActiveModel::禁止BooksController中的属性错误#创建 ActiveModel::禁止属性错误 def创建 @book=book.new(params[:book])——这是第13行 如果@book.save 重定向到:action=>“list” ActiveModel::ForbiddenAttribute错误(ActiveModel::ForbiddenAttribute错误): app/controllers/books\u controller.rb:13:in'create' 呈现c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.3/lib/action\u dispatch/middleware/templates/rescues//u source.erb(0.0ms)。。。。。 它有两种型号 book.rb 类目“错误消息”的数值性 结束 subject.rb 类主题“新建” 结束 结束 私有的 def book_参数 参数要求(:书)。许可(:标题,:价格,:主题,:描述) 结束 定义编辑 @book=book.find(参数[:id]) @主题=主题。查找(:全部) 结束 def更新 @book=book.find(参数[:id]) 如果@book.update_属性(参数[:book]) 重定向到:action=>'show',:id=>@book 其他的 @主题=主题。查找(:全部) 呈现:操作=>“编辑” 结束 结束 def删除 Book.find(params[:id]).destroy 重定向到:action=>“list” 结束 def show_主题 @subject=subject.find(参数[:id]) 结束 结束 路由文件是 routes.rb LibrayWebApplication::Application.routes.draw do 获取“书籍/新” 发布“书籍/创建” 获取“书籍/列表” 获取“书籍/展览” 获取“书籍/编辑” 获取“书籍/展示主题” 结束 并且视图文件是 new.erb 添加新书 “创建“%”,ruby-on-rails-4,Ruby On Rails 4,标题: 价格: 主题: 说明 '列表'}%> 数据库包含带有字段的表,但它不存储数据,而是显示错误,因为我是新来的。它花费了我很多时间,它显示了一个带有字段标题、价格、主题和描述的表单,当我单击按钮时,显示了以下错误 I am new to ruby as well as rails, now I am developing an application using Rails4 and Eclipse IDE..the application is Libra
'列表'}%> 数据库包含带有字段的表,但它不存储数据,而是显示错误,因为我是新来的。它花费了我很多时间,它显示了一个带有字段标题、价格、主题和描述的表单,当我单击按钮时,显示了以下错误
I am new to ruby as well as rails, now I am developing an application using Rails4 and Eclipse IDE..the application is Library application the functionalists are new,create a book,show,list,delete and edit book etc.
I enter in the browser http://127.0.0.1:3000/books/new it shows a form with fields title,price,subject and description and create button when I clicked the button its showing the following error
ActiveModel::ForbiddenAttributesError in BooksController#create
ActiveModel::ForbiddenAttributesError
def create
@book = Book.new(params[:book])--------it is line no 13
if @book.save
redirect_to :action => 'list'
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).....
It having two models
book.rb
class Book < ActiveRecord::Base
belongs_to :subject
validates_presence_of :title
validates_numericality_of :price, :message=>"Error Message"
end
subject.rb
class Subject < ActiveRecord::Base
has_many :books
end
the controller is
books_controller.rb
class BooksController < ApplicationController
def list
@books = Book.find(:all)
end
def show
@book = Book.find(params[:id])
end
def new
@book = Book.new
@subjects = Subject.find(:all)
end
def create
@book = Book.new(params[:book])
if @book.save
redirect_to :action => 'list'
else
@subjects = Subject.find(:all)
render :action => 'new'
end
end
private
def book_params
params.require(:book).permit(:Title,:Price,:Subject,:Description)
end
def edit
@book = Book.find(params[:id])
@subjects = Subject.find(:all)
end
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to :action => 'show', :id => @book
else
@subjects = Subject.find(:all)
render :action => 'edit'
end
end
def delete
Book.find(params[:id]).destroy
redirect_to :action => 'list'
end
def show_subjects
@subject = Subject.find(params[:id])
end
end
route file is
routes.rb
LibrarayWebApplication::Application.routes.draw do
get 'books/new'
post 'books/create'
get 'books/list'
get 'books/show'
get 'books/edit'
get 'books/show_subjects'
end
and the view file is
new.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
#它在mysql数据库中创建了books表
类目32,:null=>false
t、 列:价格,:浮动
t、 列:subject_id,:integer
t、 列:说明,:文本
t、 列:创建时间::时间戳
结束
结束
def自动关闭
放下桌子:书
结束
结束
20140318084609_subjects.rb
#它是在数据库中的subjects表中创建的
类主题“物理”
Subject.create:name=>“数学”
结束
def自动关闭
下表:受试者
结束
我无法解决这个问题
访问该方法时,以下代码将显示主题列表
我无法解决这个问题
我在books\u controller.rb文件中使用了强参数
migration files are
migrations are created the both the tables.
20140318084539_books.rb
#its created the books table in the mysql database
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
20140318084609_subjects.rb
#it is created the subjects table in the database
class Subjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.column :name, :string
Subject.create :name => "Physics"
Subject.create :name => "Mathematics"
end
def self.down
drop_table :subjects
end end
I am unable to solve this issue
the following code will displays the list of subjects when accessing the method
I am unable to solve this issue
I used the Strong Parameters in books_controller.rb file
def book_params
params.require(:book).permit(:title,:price,:subject,:description)
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to :action => 'list'
else
@subjects = Subject.find(:all)
render :action => 'new'
end
end