ガチガチの初心者です。
投稿サイトを作成しています。
books_controller.rb
* * def new @book = Book.new end def create @book = Book.new(book_params) @book.user = current_user if @book.save! flash[:notice] = "Book was successfully created." redirect_to books_path(@book) else @books = Book.all render("books/index") end end * * * private def book_params params.require(:book).permit(:title, :profile_image, :body) end end end
_new.html.erb
<%= form_for(book) do |f| %> <div class="field"> <%= f.label :title %></br> <%= f.text_field :title, id: :book_title %> </div> <div class="field"> <%= f.label :Opinion %></br> <%= f.text_area :body, id: :book_body %> </div> <div class="creste"> <%= f.submit "Create Book" , class:"btn btn-primary btn-block" %> </div> <% end %>
book_path
book_params共にいままで普通に使えていたので困っています。
このエラーが出る前にsyntax errorとしてendが足りないと言われたので最後にendをひとつ足しました。
もしかするとそれが原因なのかもしれないのですが、現状解決できません。
何かアドバイスいただければ幸いです。
books_controller.rb
class BooksController < ApplicationController skip_before_action :authenticate_user!, only: [:top, :about, :login, :signup ] def index @books = Book.all @book = Book.new end def new @book = Book.new end def create @book = Book.new(book_params) @book.user = current_user if @book.save! flash[:notice] = "Book was successfully created." redirect_to books_path(@book) else @books = Book.all render("books/index") end end def show @book = Book.includes(:user).find(params[:id]) @user = @book.user. end def edit @book = Book.find(params[:id]) end def update book = Book.find(params[:id]) if book.update(book_params) flash[:notice] = "Book was successfully updated." redirect_to book_path(book) else @book = book render("books/edit") end end def destroy book = Book.find(params[:id]) book.destroy flash[:notice] = "Book was successfully destroyed." redirect_to books_path end private def book_params params.require(:book).permit(:title, :profile_image, :body) end def user_params params.require(:user).permit(:name, :introduction) end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/08 03:48
2020/08/08 03:51