前提・実現したいこと
いつもお世話になっております。今回もよろしくお願いします
app/views/users/show.html.haml
上段のlink_toからapp/views/users/follow.html.haml
(フォロー中ユーザー・フォロワーを表示するページ)に遷移したいです。
= link_to(@user.followings.count,followings_user_path(@user.id))
のように書いたのですが、'/users/1/followers'のようなstringのキーは存在しませんと怒られます。
この場合は、、'/users/1/followers'が文字列として認識されているということなんでしょうか?お知恵をお貸しいただけると幸いです。
エラーメッセージ
NoMethodError in Users#show
Showing /home/ec2-user/environment/new_app2/app/views/users/show.html.haml where line #9 raised:
undefined method `stringify_keys' for "/users/1/followers":String
Parameters:{"id"=>"1"}
Started GET "/tours/1" for 160.15.13.173 at 2019-08-06 06:42:01 +0000 Cannot render console from 160.15.13.173! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by ToursController#show as HTML Parameters: {"id"=>"1"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 91], ["LIMIT", 1]] Tour Load (0.1ms) SELECT "tours".* FROM "tours" WHERE "tours"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] Rendering tours/show.html.haml within layouts/application User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] (0.2ms) SELECT COUNT(*) FROM "likes" WHERE "likes"."tour_id" = ? [["tour_id", 1]] Like Load (0.1ms) SELECT "likes".* FROM "likes" WHERE "likes"."user_id" = ? AND "likes"."tour_id" = ? LIMIT ? [["user_id", 91], ["tour_id", 1], ["LIMIT", 1]] Rendered likes/_likes_box.html.haml (7.1ms) Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."tour_id" = ? [["tour_id", 1]] Rendered comments/_index.html.haml (11.4ms) Rendered comments/_form.html.haml (13.3ms) Tour Exists (0.2ms) SELECT 1 AS one FROM "tours" INNER JOIN "favorites" ON "tours"."id" = "favorites"."tour_id" WHERE "favorites"."user_id" = ? AND "tours"."id" = ? LIMIT ? [["user_id", 91], ["id", 1], ["LIMIT", 1]] Rendered favorites/_favorites_form.html.haml (42.0ms) Rendered tours/show.html.haml within layouts/application (108.5ms) Rendered layouts/_head.html.haml (634.5ms) Rendered shared/_search_form.html.haml (1.5ms) Rendered layouts/_header.html.haml (10.3ms) Rendered layouts/_flash.html.haml (1.2ms) Completed 200 OK in 853ms (Views: 766.5ms | ActiveRecord: 3.7ms) Started GET "/users/1" for 160.15.13.173 at 2019-08-06 06:42:04 +0000 Cannot render console from 160.15.13.173! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by UsersController#show as HTML Parameters: {"id"=>"1"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 91], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] Rendering users/show.html.haml within layouts/application (0.1ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? [["user_id", 1]] (0.1ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."user_id" WHERE "relationships"."follow_id" = ? [["follow_id", 1]] Rendered users/show.html.haml within layouts/application (14.7ms) Completed 500 Internal Server Error in 20ms (ActiveRecord: 0.7ms) ActionView::Template::Error (undefined method `stringify_keys' for "/users/1/followers":String): 6: = link_to(@user.followings.count,followings_user_path(@user.id)) 7: %p 8: フォロワー: 9: = link_to(@user.followers.count,root_path,followers_user_path(@user.id)) 10: = render('relationships/follow_form',user: @user) 11: -@tours.each do |tour| 12: .card.my-1 app/views/users/show.html.haml:9:in `_app_views_users_show_html_haml___2615651576268760598_78742120'
app/config/routes.rb
ruby
1Rails.application.routes.draw do 2 3 4 5 resources :users, :only => [:show,:follow] 6 resources :tours, :only => [:index,:new,:create,:show, 7 :edit,:update,:destroy] 8 resources :relationships, only: [:create, :destroy] 9 10 resources :users do 11 member do 12 get :followings, :followers 13 end 14 end 15 resources :tours do 16 resources :likes, :only => [:create,:destroy] 17 resources :favorites, only: [:create, :destroy] 18 resources :comments, :only => [:create,:destroy] 19 end 20 21end 22
app/views/users/show.html.haml
ruby
1%h2.page_title.mb-2 2 =@user.username 3 のページ 4%p 5 フォロー: 6= link_to(@user.followings.count,followings_user_path(@user.id)) 7%p 8 フォロワー: 9= link_to(@user.followers.count,root_path,followers_user_path(@user.id)) 10= render('relationships/follow_form',user: @user) 11-@tours.each do |tour| 12 .card.my-1 13 .card-body 14 %h4.card-title 15 %smallタイトル: 16 #{tour.tourname} 17 %h6.card-subtitle.mb-2.text-muted 18 %small製作者: 19 =link_to(tour.user.username,user_path(tour.user.id)) 20 %p.card-text 21 = truncate(tour.tourcontent,separator: '.',lendth:40) do 22 =link_to("続きを読む",tour_path(tour.id)) 23 24 25 = link_to "詳細",tour_path(tour.id),class:"card-link" 26 -if current_user == tour.user 27 = link_to "編集",edit_tour_path,class:"card-link" 28 = link_to "削除","/tours/#{tour.id}",class:"card-link",method: :delete
app/views/users/follow.html.haml
ruby
1- provide(:title, @title) 2.row 3 %aside.col-md-4 4 %section.user_info 5 %h1= @user.usernamename 6 %span= link_to "view my profile", @user 7 %span 8 %b Tours: 9 = @user.tours.count 10 %section.stats 11 - if @users.any? 12 .user_avatars 13 - @users.each do |user| 14 = user.username 15 = user.created_at 16 .col-md-8 17 %h3= @title 18 - if @users.any? 19 %ul.users.follow 20 = render @users 21 = paginate @users 22
app/controller/users_controller.rb
ruby
1class UsersController < ApplicationController 2before_action:check_user_login?, only:[:show] 3 def show 4 @user=User.find_by(id: params[:id]) 5 @tours=Tour.where(user_id: @user.id) 6 end 7 8 def following 9 @title = "Following" 10 @user = User.find(params[:id]) 11 @users = @user.following.page(params[:page]) 12 render 'follow' 13 end 14 15 def followers 16 @title = "Followers" 17 @user = User.find(params[:id]) 18 @users = @user.followers.page(params[:page]) 19 render 'follow' 20 end 21end
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 13 14 15 def follow(other_user) 16 unless self == other_user 17 self.relationships.find_or_create_by(follow_id: other_user.id) 18 end 19 end 20 21 def unfollow(other_user) 22 relationship = self.relationships.find_by(follow_id: other_user.id) 23 relationship.destroy if relationship 24 end 25 26 def following?(other_user) 27 self.followings.include?(other_user) 28 end 29 30 31
rails routes
Prefix Verb URI Pattern Controller#Action RailsAdmin::Engine
followings_user GET /users/:id/followings(.:format) users#followings
followers_user GET /users/:id/followers(.:format) users#followers
user GET /users/:id(.:format) users#show
![guest](/img/icon/icnUserSample.jpg)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/06 08:11