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'} %>
数据库包含带有字段的表,但是它不存储数据,而是显示错误,因为我是新手,它花了我很多时间,它显示了一个包含字段标题,价格,主题和描述的表单。单击按钮时创建按钮,显示以下错误
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
答案 0 :(得分:0)
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