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

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

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

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

8364閲覧

ajaxでのCompleted 500 Internal Server Errorを解決したい

NaojirouHisada

総合スコア60

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/02/11 09:19

編集2016/02/11 09:21

現在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')) %> ");

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

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

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

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

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

guest

回答1

0

ベストアンサー

このControllerのActionに対応するViewがないというエラーなので、
Viewではなく json を返すようにすれば問題ないと思います。
具体的には update アクションの一番最後に下記のように書きます。

# jsonに渡すハッシュは適当です。 render status: 200, json: { message: 'OK' }

投稿2016/02/12 05:40

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

NaojirouHisada

2016/02/13 09:18

アドバイスありがとうございます!!! 試した結果、 エラーはなくなりましたが、 statusの部分が更新されていませんでした。 この原因はなんなのでしょうか(^^;)?? また、 htmlで返すことはだめなのでしょうか??
退会済みユーザー

退会済みユーザー

2016/02/13 09:27 編集

すみません。 RailsのAjaxオプションでしたね。 `render status: 200, message: { }` の部分を消して、 `app/views/relationships/edit.html`を作成し内容を 下記のようにすれば描画されるかと思います。 ``` <%= @status %> ``` 上記のようにHTMLではなく単純にテキストを返したいだけなら `render json: @status` でも表示されるかと思います
NaojirouHisada

2016/03/02 07:37

返事が大変遅くなってしまい申し訳ありません。 丁寧な説明ありがとうございます!! 早速試してみます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問