My Application name is LibraryWebApplication when I accessing my new.erb file using /books/new its giving the form then I filled the form with title,price,description then I clicked the button create its is going to following url /books/create but its is not storing data into data base its showing the following error
LoadError in BooksController#create
Unable to autoload constant BooksController, expected D:/RailsAppsExamples/LibrarayWebApplication/app/controllers/books_controller.rb to define it
the stack trace is
tarted POST "/books/create" for 127.0.0.1 at 2014-03-18 16:24:39 +0530
LoadError (Unable to autoload constant BooksController, expected D:/RailsAppsExamples/LibrarayWebApplication/app/controllers/books_controller.rb to define it):
activesupport (4.0.3) lib/active_support/dependencies.rb:464:in `load_missing_constant'
activesupport (4.0.3) lib/active_support/dependencies.rb:184:in `const_missing'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:680:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call
I have 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
What is mean by Unable to auto load constant Books Controller I am helpless to find/detect the problem in my application
I used Strong Parameters as I am using Rails4 and I am new to ruby and rails
is it require to write the Strong Parameters..is to match the permit parameters with database tables parameters
my controller is
books_controller.rb
class BookController < 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)
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
my routes.rb is
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
new.erb file is
<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
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
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
是否需要强参数代码中的所有列名称,您能告诉我如何为我的应用程序编写强参数..我无法继续前进 我有两本表和主题
books columns are as
follows
id,title,price,subject_id, Description,created_at
and subjects table columns are
id,name
是否需要强参数代码中的所有列名称,您能告诉我如何为我的应用程序编写强参数..我无法继续前进 我有两本书和主题 我无法在Books Controller中的Load Error应用程序中找到问题#create是否需要编写Strong Parameters ..来匹配permit参数和数据库表参数
答案 0 :(得分:1)
控制器的类名是BookController而不是BooksController