前提・実現したいこと
いつもお世話になっております。今回もよろしくお願いします。
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
回答1件
あなたの回答
tips
プレビュー