2014-12-04 13 views
0

我正試圖在用戶之間爲我的應用程序實現關注/取消關注功能。我正在使用devise + omniauth並使用單獨的用戶控制器。當我點擊關注按鈕時,它不會對數據進行任何處理,這些數據應該包括以下/關注者等。在控制檯中我得到這些錯誤rails 4試圖實現用戶之間的關注/取消關注功能,但是錯誤

Processing by RelationshipsController#create as JS 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"ReTtZn0GbB7f5LROr6FLnw40ErDDcubDa8/yLnyVd/FIhxM3qjZI8pjYDAGmuGJiv1paGVjhp/LMVmFtkcZVwQ==", "followed_id"=>"1", "commit"=>"Follow"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
    (0.1ms) begin transaction 
WARNING: Can't mass-assign protected attributes for Relationship: followed_id 
     app/models/user.rb:21:in `follow' 
     app/controllers/relationships_controller.rb:7:in `create' 
    (0.1ms) commit transaction 
    Relationship Load (0.3ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = ? AND "relationships"."followed_id" = ? LIMIT 1 [["follower_id", 2], ["followed_id", 1]] 
    Rendered users/_unfollow.html.erb (11.5ms) 
    Rendered relationships/create.js.erb (13.9ms) 
Completed 500 Internal Server Error in 38ms 

ArgumentError (First argument in form cannot contain nil or be empty): 
    app/views/users/_unfollow.html.erb:1:in `_app_views_users__unfollow_html_erb___2456782614560778317_70319555077280' 
    app/views/relationships/create.js.erb:1:in `_app_views_relationships_create_js_erb__3841758023410302021_45366420' 
    app/controllers/relationships_controller.rb:8:in `create' 

我行的代碼如下

relationship_controller

class RelationshipsController < ApplicationController 
    before_filter :authenticate_user! 

    def create 
    @user = User.find(params[:followed_id]) 
    current_user.follow(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 

    def destroy 
    @user = Relationship.find(params[:id]).followed 
    current_user.unfollow(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 
end 

_follow_form.html.erb

<div id="follow_form"> 
    <% if current_user.following?(@user) %> 
    <%= render 'unfollow' %> 
    <% else %> 
    <%= render 'follow' %> 
    <% end %> 
    </div> 

_unfollow.html.erb

<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id), 
      html: { method: :delete }, 
      remote: true) do |f| %> 
    <%= f.submit "Unfollow", class: "btn" %> 
<% end %> 

創建/銷燬.js.html

$("#follow_form").html('<%= escape_javascript(render('users/unfollow')) %>') 
$("#followers").html('<%= @user.followers.count %>') 

$("#follow_form").html('<%= escape_javascript(render('users/follow')) %>') 
$("#followers").html('<%= @user.followers.count %>') 

預先感謝您..我渴望想出解決辦法...

編輯我的錯誤引用通告

+0

在Rails 4中,你必須白名單params。使用以下內容:def private_params params [:yourmodel] permit(:yourkeys,:anotherkey] end – 2014-12-04 18:57:07

回答

0

你的錯誤是提交的表格是空的/零。

這是由於不接受表單及其屬性。

添加到您的控制器

private 
    def private_params 
     params[:yourmodel].permit(:yourkeys, :anotherkey] 
end 
+0

我添加了private def private_params params [:relationship] .permit(:follower_id,:followed_id) end但仍然收到相同的錯誤。 。其他建議?感謝您的初始投入 – ahk 2014-12-04 19:11:47

0

我能夠通過我的舊的寶石消除protected_attributes寶石來解決這個問題。它是導軌3的剩餘部分,並與新導軌4發生碰撞。所以,如果有人遇到問題。請刪除這個寶石,並注意,軌4現在使用強參數。 謝謝。

0

我和你有幾乎相同的錯誤,我無法準確記得。

對我來說,一切都很好,跟隨/取消關注功能工作,但當我取消關注用戶時,我總是得到這種錯誤。

對我來說,解決辦法是改變取消關注form_forlink_to

<%= link_to current_user.active_relationships.find_by(followed_id: @user.id), remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %> 
    <button class="btn" type="submit">Unfollow</button> 
<% end %> 

和錯誤消失了。

查看此simple & quick tutorial瞭解更多信息。

相關問題