Home>
It's been a week since I started studying.
I want to save using the create action, but I get an error.
I'm sorry for lack of study, but I would like to know.
NoMethodError in BooksController # create
undefined method `book'for #<Book: 0x00007fa5e8083460>
routes
root to:'books # top'
get'top' =>'books # top'
get'book/new'
get'index' =>'books # index'
post'books' =>'books # create'
get'books /: id'=>'books # show', as:'book'
get'book /: id', to:'books # show'
get'books /: id/edit'=>'books # edit', as:'edit_book'
patch'books /: id'=>'books # update', as:'update_book'
delete'books /: id'=>'books # destroy', as:'destroy_book'
end
book controller
def self.book
end
def index
@books = Book.all
end
def new
@book = Book.new
end
def create
book = Book.new (book_params)
book.save
redirect_to'/ index'
end
def show
@book = Book.find (params [: id])
end
def edit
@book = Book.find (params [: id])
end
def update
book = Book.find (params [: id])
book.update (book_params)
redirect_to book_path (book.id)
end
def destroy
book = Book.find (params [: id])
book.destroy
redirect_to'/ index'
end
private
def book_params
params.require (: book) .permit (: title,: body)
end
end
index file
<% @ books.each do | book |%>
title
<span><% = book.title%></span>
body
<span><% = book.body%></span>
<p>
<% = link_to "show", book_path (book.id)%>
<% = link_to "edit", edit_book_path (book.id)%>
<% = link_to "destroy", destroy_book_path (book.id), method:: delete, "data-confirm" =>"Are you sure I want to delete?"%>
</p>
<% end%>
<% = form_with model: Book.new, url:'/ books', local: true do | f |%>
title
<% = f.text_field: title%>
Body
<% = f.text_area: body%>
<% = f.submit'new post'%>
<% end%>
-
Answer # 1
Related articles
- ruby on rails - create a chat feature with action cable on ec2 on rails aws
- ruby - about rails controller new action
- ruby on rails - not saved by create action
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby on rails - about deploying apps using s3 function
- ruby on rails 5 - error about rails where method
- ruby - about how to save db with rails devise
- ruby on rails - about primary key/natural key/surrogate key in db design
- ruby on rails - about nomethoderror in skilscontroller # create
- ruby on rails - about switching screen display on multiple models
- ruby - [rails] about nameerror
- ruby - [rails] i want to create a link including the data attribute with the link_to helper
- [ruby on rails] about header setting using bootstrap
- ruby - about rails routing
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby - [rails] about partial templates
- ruby on rails 5 - rails: i want to create a timetable app
- ruby on rails - about rails composite indexes
- about description of manifest file of ruby on rails
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
controller
view
How about this