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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

1374閲覧

Rails : “いいね”機能の実装でroutes.rbでエラーが出てしまう

innjera

総合スコア132

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2016/11/01 23:37

編集2016/11/01 23:44

product(model)に対し、user(model)が「イイね」をできる機能を、vote modelを作成し、実装していますが、route.rbの記載に誤りがあるのか、rails sでサーバーが立ち上がらず、添付の様なエラーが出てしまいます。route.rbの記載にミスがあるのだと思うのですが、お分かりの方、ご教示頂けますと、助かります。

イメージ説明

ruby

1Rails.application.routes.draw do 2 namespace :user do 3 root 'products#index' 4 get 'top' => 'top#index' 5 get 'login' => 'sessions#new', as: :login 6 resource :session, only:[:create, :destroy] 7 resource :account, only:[:show, :edit, :update] 8 resources :users do 9 resources :products 10 end 11 resources :products do 12 collection {get "search"} 13 user {patch "like", "unlike" } 14 collection {get "voted"} 15 end 16 end

念のため、user、model、voteのmodelsは以下の通りです。

ruby

1#product.rb 2class Product < ApplicationRecord 3 belongs_to :user 4 has_many :votes, dependent: :destroy 5 has_many :voters, through: :votes, source: :user 6 7 class << self 8 def search(query) 9 rel = order("id") 10 if query.present? 11 rel=rel.where("brand LIKE? OR memo LIKE? OR price LIKE?", "%#{query}%", "%#{query}%", "%#{query}%") 12 end 13 rel 14 end 15 end 16end

ruby

1#user.rb 2class User < ApplicationRecord 3 has_many :products, dependent: :destroy 4 has_many :votes, dependent: :destroy 5 has_many :voted_products, through: :votes, source: :product 6 7 before_validation do 8 self.email_for_index = email.downcase if email 9 end 10 11 def password=(raw_password) 12 if raw_password.kind_of?(String) 13 self.hashed_password = BCrypt::Password.create(raw_password) 14 elsif raw_password.nil? 15 self.hashed_password = nil 16 end 17 end 18 19 def votable_for?(product) 20 product && product.user != self && !votes.exists?(product_id: product.id) 21 end 22 23 class << self 24 def search(query) 25 rel = order("id") 26 if query.present? 27 rel=rel.where("email LIKE? OR family_name LIKE? OR given_name LIKE? OR family_name_kana LIKE? OR given_name_kana LIKE? OR user_name LIKE? OR address LIKE?", 28 "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%") 29 end 30 rel 31 end 32 end 33end

ruby

1#vote.rb 2class Vote < ApplicationRecord 3 belongs_to :product 4 belongs_to :user 5 6 validate do 7 unless user && user.votable_for?(product) 8 errors.add(:base, :invalid) 9 end 10 end 11end 12

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

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

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

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

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

guest

回答2

0

ruby

1member {patch "like", "unlike" }

が正しい書き方でした。

投稿2016/11/04 03:15

innjera

総合スコア132

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

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

0

ベストアンサー

ruby

1user {patch "like", "unlike" }

これが誤りです。

どうしたいのかがわからないので正解はわかりかねますが、とりあえずその部分を他と同様に

ruby

1collection {patch "like", "unlike" }

にすれば rails s でのサーバ起動はできると思います。

投稿2016/11/02 00:08

hana-da

総合スコア1728

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

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

innjera

2016/11/02 03:23

早速有難う御座います。サーバーは動きました。 やりたい動作を実現できない可能性もありますが、再度自己解決できなかった際は改めてお願い致します。
innjera

2016/11/04 03:16

先日ご回答頂いた件、member {patch "like", "unlike" }が正しい記載でした。 routes.rbに記載するmemberはmodel名に紐ついでいるのではなくmemberという固有のメソッドだということを知りませんでした。。お恥ずかしい限りです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問