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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

2430閲覧

【Rails】いいね機能を実装中なんですが、コントローラにパラメータを渡せません

k_yusuke

総合スコア19

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

2クリップ

投稿2019/08/05 03:43

編集2019/08/05 04:33

前提・実現したいこと

いつもお世話になっております。今回もよろしくお願いします。
RailsでTwitterにおける「いいね!」のような機能を実装しています。いいねはfavoriteモデルです。
app/views/tours/show.html.haml中段のlink_toからfavorites_controllerにパラメータ(@tour.id)を渡したいのですが、うまく渡ってくれず、コントローラ側でparamsを使ってもデータが拾えずnilになってしまいます。 
hidden_field_tagも使ってみたのですがやはりnilになります。
皆さまの知恵をお貸しいただけると幸いです。
Rails 5.2.3,cloud9使用

発生している問題・エラーメッセージ

app/views/tours/show.html.haml

ruby

1%h2.page_title.mb-2 ツアー詳細 2.card 3 .card-body 4 %h4.card-title 5 %smallタイトル: 6 #{@tour.tourname} 7 %h6.card-subtitle.mb-2.text-muted 8 %small製作者: 9 =link_to(@tour.user.username,user_path(@tour.user.id)) 10 %p.card-text 11 = @tour.tourcontent 12 = image_tag @tour.tour_image1.to_s 13 = image_tag @tour.tour_image2.to_s 14 = image_tag @tour.tour_image3.to_s 15 %p.card-text 16 -if liked?(@tour) 17 = link_to('いいねを取り消す',tour_like_path(@tour.id),method: :delete) 18 -else 19 = link_to('いいねする',tour_likes_path(@tour.id),method: :post) 20.card#comments_area 21 = render partial: 'comments/index', 22 locals: { comments: @comments } 23.card 24 = render partial: 'comments/form', 25 locals: { comment: @comment, tour: @tour } 26

app/controller/likes_controller.rb

ruby

1class LikesController < ApplicationController 2before_action:check_user_login?, only:[:create,:destroy] 3 4 def create 5 @tour=Tour.find_by(id: params[:id]) 6 @like =Like.create(user_id: current_user.id,tour_id: @tour.id) 7 redirect_back(fallback_location: root_path) 8 end 9 10 def destroy 11 @like = current_user.likes.find_by(tour_id: @tour.id) 12 @like.destroy 13 redirect_back(fallback_location: root_path) 14 end 15end

app/model/tour.rb

ruby

1class Tour < ApplicationRecord 2 3 belongs_to :user 4 has_many :likes, dependent: :destroy 5 has_many :liked_users, through: :likes, source: :user 6 has_many :comments, dependent: :destroy 7 has_many :favorites, foreign_key: 'tour_id', dependent: :destroy 8 has_many :users, through: :favorites 9 end

app/model/user.rb

ruby

1class User < ApplicationRecord 2 3has_many :tours, dependent: :destroy 4has_many :comments, dependent: :destroy 5has_many :likes, dependent: :destroy 6has_many :liked_tours, through: :likes, source: :tour 7has_many :favorites 8has_many :favtours, through: :favorites, source: :tour 9has_many :relationships 10has_many :followings, through: :relationships, source: :follow 11has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' 12has_many :followers, through: :reverse_of_relationships, source: :user 13end

app/model/like.rb

ruby

1class Like < ApplicationRecord 2 belongs_to :user 3 belongs_to :tour 4 validates_uniqueness_of :tour_id, scope: :user_id 5 validates :user_id,presence:true 6 validates :tour_id,presence:true 7end

app/config/routes.rb

ruby

1Rails.application.routes.draw do 2 3 4 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 5 root 'static_pages#home' 6 get '/about'=>'static_pages#about' 7 get '/search'=>'tours#search' 8 9 devise_for :users, controllers: { 10 registrations: 'users/registrations', 11 12 sessions: "users/sessions", 13 omniauth_callbacks: "users/omniauth_callbacks", 14 15 } 16resources :users, :only => [:show] 17resources :tours, :only => [:index,:new,:create,:show, 18 :edit,:update,:destroy] 19resources :tours do 20resources :likes, :only => [:create,:destroy] 21resources :comments, :only => [:create,:destroy] 22end 23resources :relationships, only: [:create, :destroy] 24resources :favorites, only: [:create, :destroy] 25 26 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 27end 28

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

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

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

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

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

hellomartha

2019/08/05 05:22

エラーメッセージは特に出ていないということですかね? rails routes の出力を追記してみてください。
k_yusuke

2019/08/05 13:13

hellomarthaさんご回答ありがとうございます。 無事解決いたしました!!ただ、なぜ成功したのかあまり理解ができていないので、これから深堀りします!
guest

回答1

0

ベストアンサー

Favoriteのことでしたら、
routes.rbを見たところ、
resources :likes は、 resources :toursに囲まれているのに対して、
resources :favorites は囲まれていないようです ????

もし、 Like が既に期待通りに実装されていて
Favoriteも同様な実装を行いたいようなイメージであれば、
ルーティングを見直してみるとパラメータで tours_id は取得できるかと存じます。

ただ、気になる点としまして、

RailsでTwitterにおける「いいね!」のような機能を実装しています。いいねはfavoriteモデルです。

app/views/tours/show.html.haml中段のlink_toからfavorites_controllerにパラメータ(@tour.id)を渡したいのです

と記載していただいていますが、
引用していただいているコードなどを見たところ、
Like と Favorite が混同しているように見受けられました、、、、
そのあたりも、共有していただけますと幸いです????

投稿2019/08/05 04:21

fshun

総合スコア261

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

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

k_yusuke

2019/08/05 04:42 編集

fshunさん、丁寧に回答していただきありがとうございます。 >Like と Favorite が混同しているように見受けられました、、、、 おっしゃる通りです..質問文のlikeとfavoriteが混同されていたので訂正いたしました、すみません! (app/model/favorite.rb⇒app/model/like.rbへ変更) 実装したいのはlikeモデルによるいいね機能です。favoriteモデルは別機能実装時のものなので無視していただいて大丈夫です! ルーティングは`resources :tours`に囲われているのですがなぜかパラメータが拾えません...
fshun

2019/08/05 05:31

訂正ありがとうございます???? なるほどです。。。 であれば、パラメータの値を取得する際の ` params[:key] ` の部分が間違えている可能性が高いのかなと思いました。 取り急ぎ、あくまでも予想になりますが、、、 具体的には、 ` app/controller/likes_controller.rb ` の create ``` @tour=Tour.find_by(id: params[:id]) ``` の params 部分を ` params[:tour_id] `に変更しても、 期待する動きになりませんでしょうか。 このような事例のデバッグ方法に、 https://qiita.com/silmisilon/items/8e08435204d8d08d09ff のような方法を取ると、ひとつ追いかけてデバッグができるので おすすめです????
k_yusuke

2019/08/05 13:25

あ!!!!!!!できました!!!!!!!本当に助かりました!!!ありがとうございます!!!! ただ、まだ理解が浅いところがあって、app/views/tours/show.html.haml中段のlink_toからコントローラに送るパラメータは、tour_likes_pathに忍ばせた@tour.idのはずなのに、favorites_controllerのcreateアクションで受け取る際はtour_idとなる理由(頓珍漢だったらすみません...)がまだ理解できてないようなので、深堀してみます!!!! binding.pryによるデバッグは試したことがあったのですが、他のpryも便利そうですね~!勉強になります!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問