現在facebookのような友達申請機能を作成しています。
友達関係を管理する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
と各ユーザーを管理するUserモデルがあります。
この二つの関係性はこうです。
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) following_relationships.find_by(followed_id: other_user.id).destroy end # あるユーザーをフォローしているかどうか? def following?(other_user) following_users.include?(other_user) end end
statusを作ることにより、
友達申請->承認->友達成立という流れです。
友達申請->申請中までは良いのですが、
ajaxを使い、
Relationshipsのstatusをallow(承認)に更新処理をすると、
Completed 500 Internal Server Error in 93ms (ActiveRecord: 0.9ms) ActionView::Template::Error (undefined method `each' for nil:NilClass): 4: 5: 6: <% loop do 7: follower = followers.next 8: follower_relationship = follower_relationships.next %> 9: 10: <% if follower_relationship.status == 'allow' %> app/views/users/_follow_update.html.erb:7:in `next' app/views/users/_follow_update.html.erb:7:in `block in _app_views_users__follow_update_html_erb__922133743095404908_69892989505320' app/views/users/_follow_update.html.erb:6:in `loop' app/views/users/_follow_update.html.erb:6:in `_app_views_users__follow_update_html_erb__922133743095404908_69892989505320' app/views/relationships/update.js.erb:1:in `_app_views_relationships_update_js_erb__3383092756043455873_69893008020780' Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.5ms) Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.3ms) Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (54.7ms)
このようなエラーが発生しました。
これを解決し、
うまく更新処理ができるようにしたいのですが、
できません。
お手数おかけしますが、
アドバイス宜しくお願いします。
友達申請をされたことを知らせる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 %> <p><%= follower.first_kana + "さんが友達申請をしてきました。" %><br /> <%= "友達申請を承認しますか?" %></p> <%= form_for(follower_relationship,:url =>{ :controller => "relationships" , :action => "update" }, remote: true) do |f| %> <%= hidden_field_tag :follow_id , follower_relationship.follow_id %> <%=f.hidden_field :status %> <%= f.submit "承認"%> <% end %> <% end %> <% end %>
この更新を行うrelationshipコントローラのupdateメソッド
def update @status = Relationship.find_by(params[:follow_id]) if @status.update(:status => 'allow') else flash[:danger] = 'missing' end end
ajax操作を行うための記述のupdate.js.erb
$("#information").html("<%= escape_javascript(render ('users/follow_update')) %> ");
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/13 09:18
退会済みユーザー
2016/02/13 09:27 編集
2016/03/02 07:37