本の投稿ができるアプリを作成しています。
新規投稿後に/users/:idのページへリダイレクトしたいのですが、そこでエラーが発生しております。
NoMethodError in BooksController#create undefined method `id' for nil:NilClass Extracted source (around line #8): 6 book.user_id = current_user.id 7 book.save! 8 redirect_to user_url(@user.id)←エラー箇所 9 end 10 11 def index
初めはログインができていないことが原因かと思いましたができており、一度ログアウトした後に再ログインすると投稿が表示されます。
redirect_toでidの取得ができていないと予想しましたが、解決できませんでした。
books_controller.rb class BooksController < ApplicationController before_action :authenticate_user!, only: [:show] def create book = Book.new(book_params) book.user_id = current_user.id book.save! redirect_to user_url(@user.id) end def index end def show @book = Book.find(params[:id]) book = book.all end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) book.update(book_params) redirect_to user_path(@user) end private def book_params params.require(:book).permit(:title, :body) end end
users/show.html.erb <%= render 'shared/header' %> <div class="top"> <div class="container"> <div class="row"> <div class="col-lg-3"> <p>User info</p> <%= attachment_image_tag @user, :profile_image, :fill, 250, 250, format: 'jpeg', fallback: "no_image.jpg", size:'250x250' %> <table class="table"> <tr> <th>name</th> <th><%= current_user.name %></th> </tr> <tr> <th>introduction</th> <th><%= current_user.introduction %></th> </tr> </table> <%= link_to edit_user_path, class: "btn btn-default" do %> <i class="fa fa-wrench"></i> <% end %> <p>New book</p> <%= form_for @book do |f| %> <p>Title</p> <%= f.text_field :title %> <p>Opinion</p> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %> </div> <div class="col-lg-9"> <p>Books</p> <table class="table table-hover"> <thead> <tr> <th></th> <th>Title</th> <th>Opinion</th> </tr> </thead> <% @books.each do |book| %> <tbody> <tr> <td><%= book.user_id %><%= attachment_image_tag @user, :profile_image, :fill, 50, 50, format: 'jpeg', fallback: "no_image.jpg", size:'50x50' %><br><%= book.user.name %></td> <td><%= book.user_id %><%= book.title %></td> <td><%= book.user_id %><%= book.body %></td> </tr> </tbody> <% end %> </table> </div> </div> </div> </div> <%= render 'shared/footer' %>
user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :books, dependent: :destroy attachment :profile_image end
book.rb class Book < ApplicationRecord belongs_to :user end
$ rake routes Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create root GET / top#top home_about GET /home/about(.:format) top#home users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy books GET /books(.:format) books#index POST /books(.:format) books#create new_book GET /books/new(.:format) books#new edit_book GET /books/:id/edit(.:format) books#edit book GET /books/:id(.:format) books#show PATCH /books/:id(.:format) books#update PUT /books/:id(.:format) books#update DELETE /books/:id(.:format) books#destroy refile_app /attachments #<Refile::App app_file="/home/ec2-user/.rvm/gems/ruby-2.6.3/bundler/gems/refile-46b4178654e6/lib/refile/app.rb">
お気づきの点がありましたらご指摘をお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/12/05 14:28
2019/12/05 21:52
退会済みユーザー
2019/12/06 07:47
2019/12/06 07:52
退会済みユーザー
2019/12/06 08:37