Rails 4.2.6
ruby 2.3.1
mysql
で出会い系のWEBアプリケーションを勉強を兼ねてで構築しています。
rails初心者です。
###やりたいこと
ユーザーのshowアクションから
ユーザー同士がお気に入りボタンを押してお気に入りに追加できるようにしたい。
######ルーティング
ruby
1 Rails.application.routes.draw do 2 #devise_for :users 3 devise_for :users do 4 member do 5 get :follow 6 get :unfollow 7 end 8 end 9 root 'homes#index' #root_path 10 get 'flow' => 'flows#index' 11 get 'messages' => 'messages#index' 12 get 'profiles' => 'profiles#index' #プロフィールの一覧 13 get 'profiles/show' => 'profiles#show' #プロフィールの詳細内容 14 get 'profiles/new' => 'profiles#new' #プロフィールの登録画面 15 post 'profiles' => 'profiles#create' #プロフィールの登録画面 16 get 'profiles/:id/edit' => 'profiles#edit' 17 patch 'profiles/' => 'profiles#update' 18 get 'users/:id' => 'users#show' #mypageへのルーティング 19 get 'users' => 'users#index' #mypageへのルーティング 20 end
######Model
Userモデル(Deviseのgemを利用)
- id
- password
- nickname
ruby
1class User < ActiveRecord::Base 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :trackable, :validatable 6 has_one :profile 7 acts_as_followable # フォロワー機能 8 acts_as_follower # フォロー機能 9end
Profileモデル
- id
- user_id
- age
- mytype
- pref
- text
ruby
1class Profile < ActiveRecord::Base 2 mount_uploader :image, ImageUploader 3 belongs_to :user 4end
Followモデル(acts_as_followerのgemを利用)
- id
- followable_id
- followable_type
- follower_id
- follower_type
ruby
1class Follow < ActiveRecord::Base 2 3 extend ActsAsFollower::FollowerLib 4 extend ActsAsFollower::FollowScopes 5 6 # NOTE: Follows belong to the "followable" interface, and also to followers 7 belongs_to :followable, :polymorphic => true 8 belongs_to :follower, :polymorphic => true 9 10 def block! 11 self.update_attribute(:blocked, true) 12 end 13 14end 15
以上がmodel,db,各モデル.rbです。
ユーザーモデルとプロフィールモデルがアソシエーションで結ばれています。
######Controller
user_controller.rb
ruby
1class UsersController < ApplicationController 2 before_action :move_to_index, only: :index 3 4 def index 5 @user = User.all 6 end 7 8 def show 9 user = User.find(params[:id]) 10 @nickname = current_user.nickname 11 @email = current_user.email 12 @profile = user.profile 13 @friend = User.find(params[:id]) 14 ##以下はフォロー用 15 @me = current_user 16 @all = current_user.all_following 17 @user = User.find(params[:id]) 18 end 19 20 def follow 21 @user = User.find(params[:id]) 22 current_user.follow(@user) 23 redirect_to root_path 24 end 25 26 private 27 def move_to_index 28 redirect_to '/users/sign_in',:flash => {:alart => "ご利用にはログインが必要です。会員登録がまだの方は会員登録を行って下さい。"} unless user_signed_in? 29 end 30 31end
profiles_controller.rb
ruby
1class ProfilesController < ApplicationController 2 3 def index 4 end 5 6 def new 7 @nickname = current_user.nickname 8 @email = current_user.email 9 end 10 11 def create 12 Profile.create(birthday: profile_params[:birthday], bodytype: profile_params[:bodytype], mytype: profile_params[:mytype], pref: profile_params[:pref], text: profile_params[:text],image: profile_params[:image], user_id: current_user.id) 13 end 14 15 def show 16 @nickname = current_user.nickname 17 @email = current_user.email 18 end 19 20 def edit 21 @profile = User.find(params[:id]) 22 @nickname = current_user.nickname 23 @email = current_user.email 24 end 25 26 def update 27 profile = Profile.find_by(user_id: current_user.id) 28 profile.update(profile_params) 29 end 30 31 private 32 def profile_params 33 params.permit(:birthday, :bodytype, :mytype, :pref, :text, :image) 34 end 35 36end
です。
views/users/show.html.erbが下記です。
長いので該当の箇所だけ記載します。
ruby
1<% if current_user.following?(@user) %> 2お気に入り解除のボタン 3<% else %> 4お気に入りに登録する 5<% end %>
上記のようにする場合どのようにしてお気に入り登録、または解除を実装すればよいでしょうか?
ちなみにacts_as_followerの使い方は下記を参考にしました。
http://qiita.com/kurkuru/items/ad25569f64e74efdac56#%E3%81%8A%E3%82%8F%E3%82%8A%E3%81%AB
初心者のコードですのでめちゃくちゃだとは思いますがご教授お願い致します。
追記---------------------------------------------------
<%= link_to "Follow", @me.follow(@user) %>
を追加するとエラーになったり、リンクをクリックしていないのにフォロー機能が勝手に動くことが有るのですが原因もわかりません。。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/24 04:44
2017/04/24 05:45
2017/04/24 06:57