ベストアンサー機能を実装したら Post_id がないと言われる
対象のエラーは以下の通りです。
バリデーションに失敗しました: Postを入力してください, Postを入力してください answer_id: params[:answer_id] ) @star.save redirect_to("/posts/#{params[:post_id]}") end end
ちなみにここでは star はベストアンサーのことです
対象のコード
<% @post.answers.each do |answer| %> <div class="posts-index-item"> <div class="post-left"> <%= link_to image_tag("/user_images/#{answer.user.image_name}"), "/users/#{answer.user.id}"%> </div> . . . . <div class="post-menus-right"> <% if answer.star %> <i class="fas fa-medal best-answer"></i> <% else %> <%= link_to("ベストアンサーにする", "/stars/#{answer.id}/create", {method: "post"}) %> <% end %> </div> </div>
class Post < ApplicationRecord def answers return Answer.where(post_id: self.id).order(created_at: :desc) end end
Post modelでは Answers に(post_id: self.id)を渡しています。なので
<%= link_to("ベストアンサーにする", "/stars/#{answer.id}/create", {method: "post"}) %>
このリンクを踏んだ時にpost_idが star Contollerに受け渡しされるはずですが、なぜかうまくいきません。
star Contollerの中身は以下の通りです。
class StarsController < ApplicationController def create @star = Star.new( user_id: params[:user_id], post_id: params[:post_id], answer_id: params[:answer_id] ) @star.save redirect_to("/posts/#{params[:post_id]}") end end
Answerは user_id と post_id
Straは user_id と post_id と answer_id を持っています。
お力を貸していただけたら幸いです。
rake routes コマンドを実行した結果も追記していただけますか?
Prefix Verb URI Pattern Controller#Action
unions_create POST /unions/create(.:format) unions#create
unions_new GET /unions/new(.:format) unions#new
POST /unions/:id/destroy(.:format) union_users#destroy
POST /unions/:id/create(.:format) union_users#create
GET /unions/:id(.:format) unions#show
GET /unions/:id/posts(.:format) unions#posts
GET /users/:id/answers(.:format) users#answers
POST /answers/:post_id/update(.:format) answers#update
POST /answers/:post_id/create(.:format) answers#create
POST /evaluations/:post_id/create(.:format) evaluations#create
POST /evaluations/:post_id/destroy(.:format) evaluations#destroy
POST /stars/:answer_id/create(.:format) stars#create
GET /users/:id/likes(.:format) users#likes
POST /likes/:post_id/destroy(.:format) likes#destroy
POST /likes/:post_id/create(.:format) likes#create
POST /users/:id/update(.:format) users#update
GET /users/:id/edit_security(.:format) users#security
GET /users/:id/edit(.:format) users#edit
users_create POST /users/create(.:format) users#create
signup GET /signup(.:format) users#new
users_index GET /users/index(.:format) users#index
GET /users/:id(.:format) users#show
login POST /login(.:format) users#login
logout POST /logout(.:format) users#logout
GET /login(.:format) users#login_form
posts_index_relative GET /posts/index/relative(.:format) posts#index_relative
posts_index_updated_at GET /posts/index/updated_at(.:format) posts#index_updated_at
posts_index GET /posts/index(.:format) posts#index
posts_new GET /posts/new(.:format) posts#new
GET /posts/:id(.:format) posts#show
posts_create POST /posts/create(.:format) posts#create
GET /posts/:id/edit(.:format) posts#edit
POST /posts/:id/update(.:format) posts#update
POST /posts/:id/destroy(.:format) posts#destroy
GET /posts/:id/answer_edit(.:format) posts#answer_edit
GET /:department/all(.:format) users#departmentall
GET /:subject/alll(.:format) users#subjectall
GET /:uni/index(.:format) users#uni
GET /:uni/:subject/index(.:format) users#subject
GET /:uni/:subject/:department/index(.:format) users#department
GET / home#top
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
satoshih さんありがとうございます
rake routes の結果は以上になります
回答1件
あなたの回答
tips
プレビュー