質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

681閲覧

Railsでベストアンサー機能のpost_idの受け渡しができない

atage517

総合スコア36

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/07/21 06:35

編集2020/07/21 06:36

ベストアンサー機能を実装したら 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 を持っています。

お力を貸していただけたら幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

satoshih

2020/07/21 07:21

rake routes コマンドを実行した結果も追記していただけますか?
atage517

2020/07/21 07:29

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
atage517

2020/07/21 07:29

satoshih さんありがとうございます rake routes の結果は以上になります
guest

回答1

0

ベストアンサー

ソースを見る限り、user_idとpost_idをリクエスト時に送っていないように見えます。

修正案1 post_idとuser_idをanswerから取得する。

diff

1class StarsController < ApplicationController 2 def create 3+ answer = Answer.find(params[:answer_id]) 4 @star = Star.new( 5- user_id: params[:user_id], 6- post_id: params[:post_id], 7+ user_id: answer.user.id, 8+ post_id: answer.post.id, 9 answer_id: params[:answer_id] 10 ) 11 12 13 @star.save 14 redirect_to("/posts/#{params[:post_id]}") 15 end 16end

※answerに対してpostとuserが一つずつ紐付いている想定で書いてます。

修正案2 リクエストパラメータにuser_idとpost_idを含める。
<%= link_to("ベストアンサーにする", "/stars/#{answer.id}/create", {method: "post"}) %>

<%= link_to("ベストアンサーにする", "/stars/#{answer.id}/create", method: "post", params: { user_id: answer.user.id, post_id: @post.id}) %>

のようにし、paramsにuser_idとpost_idを含める。

投稿2020/07/21 07:50

satoshih

総合スコア797

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

atage517

2020/07/21 08:24

できました!!! ありがとうございます!!!本当に感謝します。 最後に一つ質問なのですが answer = Answer.find(params[:answer_id]) とは何かの省略の形でしょうか?たまにこのような書き方を見るのですが Progateで習った際にはいつも answer = Answer.find_by(id: params[:answer_id])のような書き方をしていました。
satoshih

2020/07/21 08:42

findはidで検索し、見つからない場合はエラーになるメソッドです。 find_byはkeyを指定して検索し、見つからなければnilが返ってくるメソッドです。 仕様上必ず存在するidで検索する場合はfindを使うことが多いかもしれないです。
atage517

2020/07/21 10:02

そういうことだったんですね!何から何までありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問