Facebookのようなアプリを作成しており、
現在友達申請->友達申請待ち->お知らせに「友達承認/拒否」を表示
https://gyazo.com/d7f017add647dc208b9a9b8184b835d0
->承認or拒否を押す->お知らせページの申請部分だけを更新して表示を無くすようにしようとしています。
https://gyazo.com/cffea1831decd6ebe0a0c95e80ccd8a8
そのため、
友達管理を行うrelationshisテーブルに、
友達申請のステータスのためのstatusを作成し、
status = 'allow' =>友達というようにしています。
友達申請->友達申請待ち->お知らせに「友達承認/拒否」を表示
https://gyazo.com/d7f017add647dc208b9a9b8184b835d0
->承認or拒否を押す
までの部分は実装できたのですが、
お知らせページの申請部分だけを更新して表示を無くすところができません。
どのように実装すればよろしいのでしょうか??
ソースコードを載せますので、
お手数おかけしますがアドバイス宜しくお願いします。
relationshipsテーブル class CreateRelationships < ActiveRecord::Migration def change create_table :relationships do |t| t.references :follow, index: true t.references :followed, index: true t.string :status t.timestamps null: false t.index [:follow_id , :followed_id] , unique: true end end end
relationshipモデル class Relationship < ActiveRecord::Base belongs_to :follow , class_name: "User" belongs_to :followed , class_name: "User" end ユーザーモデル。 class User < ActiveRecord::Base has_one :member has_many :microposts has_many :comments has_many :following_relationships, class_name: "Relationship" , foreign_key: "follow_id" , dependent: :destroy has_many :following_users , through: :following_relationships , source: :followed has_many :follower_relationships, class_name: "Relationship" , foreign_key: "followed_id",dependent: :destroy has_many :follower_users, through: :follower_relationships , source: :follow has_secure_password accepts_nested_attributes_for :member # 他のユーザーをフォローする def follow(other_user ,status) following_relationships.create(followed_id: other_user.id , status: status.status) end # フォローしているユーザーをアンフォローする def unfollow(other_user) if following_relationships.exists?(:followed_id => other_user.id) following_relationships.find_by(:followed_id => other_user.id).destroy else follower_relationships.find_by(follow_id: other_user.id).destroy end end # あるユーザーをフォローしているかどうか def following?(other_user) following_users.include?(other_user) end # あるユーザーにフォローされているかどうか def follower?(other_user) follower_users.include?(other_user) end end
友達申請をされたことを知らせるinfomation.html.erb <div id="information" > <%= render 'follow_update' %> </div>
友達を承認させるための更新処理を行う_follow_update.html.erb <% followers = @followers.to_enum follower_relationships = @follower_relationships.to_enum %> <% loop do follower = followers.next follower_relationship = follower_relationships.next %> <% if follower_relationship.status == 'allow' %> <% else %> <hr > <p><%= follower.first_kana + "さんが友達申請をしてきました。" %><br /> <%= "友達申請を承認しますか?" %></p> <%= form_for(follower_relationship,:url =>{ :controller => "relationships" , :action => "update", :id => "#{follower_relationship.id}" }, remote: true) do |f| %> <%= p follower_relationship.follow_id %> <%= hidden_field_tag :follow_id , follower_relationship.follow_id %> <%=f.hidden_field :status %> <%= f.submit "承認"%> <hr > <% end %> <% end %> <% end %>
この更新を行うrelationshipコントローラのupdateメソッド def update @status = Relationship.find_by(params[:follow_id]) if @status.update(:status => 'allow') render json: @status else flash[:danger] = 'missing' end end
ajax操作を行うための記述のupdate.js.erb $("#information").html("<%= escape_javascript(render ('users/follow_update')) %> ");
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/29 16:02
2016/03/29 17:20
2016/03/29 17:33
2016/03/30 00:10