2016-03-30 155 views
-2

我不斷收到這個煩人的錯誤,我無法解決它。我最近發表了一個關於同一主題的問題,並沒有得到任何幫助。未定義的方法`cliq_requests'爲零:NilClass

我希望用戶請求加入一個組。 Cliqs =組。我所有的控制檯測試看起來都是正確的,但我似乎無法找到解決我的問題的方法。該協會正在顯示,但我似乎無法得到更新/接受方法運行。

這讓我瘋狂!我該如何解決?

這裏是我的代碼:

我的模型:

class User < ActiveRecord::Base 
has_many :uploads 

has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy 

has_many :cliq_memberships, dependent: :destroy 
has_many :cliqs, through: :cliq_memberships 

has_many :cliq_requests, dependent: :destroy 
... 
end 


class Cliq < ActiveRecord::Base 
belongs_to :owner, class_name: 'User' 

has_many :cliq_memberships, dependent: :destroy 
has_many :members, through: :cliq_memberships, source: :user 

has_many :cliq_requests, dependent: :destroy        #cliq_request_sender 
has_many :pending_members, through: :cliq_requests, source: :user, foreign_key: 'user_id' 
end 


class CliqRequest < ActiveRecord::Base 
#from 
belongs_to :user 
#to 
belongs_to :cliq 

#validate :not_member 

#validate :not_pending 

def accept 
    cliq.members << pending_member 
    destroy 
end 
end 

我的控制器:

class CliqRequestsController < ApplicationController 

def index 
    #incoming 
    #@cliq_requests_received = CliqRequest.where(cliq: cliq) 
    #outgoing 
    #@cliq_requests_sent = current_user.cliq_requests 
end 

def show 
end 

def create 
    cliq = Cliq.find_by(params[:id]) 
    @cliq_request = current_user.cliq_requests.new(cliq: cliq) 


    if @cliq_request.save 
     redirect_to current_user #change to cliqs/cliq path later 
    else 
     redirect_to cliq_path 
    end 
end 

def update 
    @cliq = Cliq.find_by(id: params[:cliq_id]) 
    @cliq_request = @cliq.cliq_requests.find_by(id: params[:id]) 
    @cliq_request.accept 
end 

def destroy 
    @cliq_request.destroy 
end 
end 

筆者認爲:

<h1><%= @cliq.name %></h1> 

<%= link_to 'Request to join Cliq', '/cliqs/:cliq_id/cliq_requests', :method => :post %> 

<% @cliq_members.each do |cliq_member| %> 
<ul><%= link_to cliq_member.username, user_path(cliq_member) %></ul> 
<% end %> 

<% if @current_user = @cliq.owner %> 
<% @cliq.pending_members.each do |pending_member| %> 
<ul><%= link_to pending_member.username, user_path %> 
<%= link_to "Accept", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :put %> 
<%= link_to "Deny", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :delete %> 
</ul> 
<% end %> 
<% end %> 

我的路線:

resources :cliqs do 
    resources :cliq_requests 
end 
+0

你在哪一行得到那個錯誤? – Pavan

+0

第32行:更新CliqRequestsController。它說「@cliq_request = @ cliq.cliq_requests.find_by(id:params [:id])」。 –

+0

應該注意的是,我已經嘗試了許多配置,但無濟於事。我也嘗試了許多不同的路線。 –

回答

1

這些行格式錯誤:

<%= link_to 'Request to join Cliq', '/cliqs/:cliq_id/cliq_requests', :method => :post %> 
<%= link_to "Accept", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :put %> 
<%= link_to "Deny", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :delete %> 

我建議你使用路徑傭工[如:如果您正在使用資源豐富的路由選擇,請選擇cliq_cliq_request_path(cliq, cliq_request)]。您可以使用耙路線尋求幫助。如果您在您的development.log或test.log中看到諸如:cliq_id:id之類的東西,並將其作爲所訪問網址的一部分,則應該將其替換爲數字。您也可以自己插入字符串(例如"/cliqs/#{cliq_id}/cliq_requests/#{cliq_request.id}"),但這通常會更多地輸入,並且隨着時間的推移肯定會更脆弱。

您的問題之一可能是您正在循環待處理成員名稱列表,這些成員名稱沒有正確形成鏈接所需的所有數據。因此,您的更新操作可能工作正常,但您可能不會傳遞正確的數據。

而且這條線:

if @current_user = @cliq.owner 

是一個賦值,因此將始終返回true。想必你的意思是==

+0

因此請求「創建」操作起作用。所有必要的數據都被保存到數據庫(cliq_id,user_id和cliq_request_id)。爲什麼它不適用於更新/銷燬方法? –

+0

我不認爲你已經提供了調用'create'方法的代碼。通常這是在_form部分。 –

+0

嘗試在更新方法中添加'puts params [:id]'或'raise params [:id]',並查看日誌或Web瀏覽器。如果它說一個數字,這個答案是錯誤的。如果它說:id,這些是你在循環中需要做的改變。 (我建議循環訪問cliq_requests,而不是掛起的成員,因爲從cliq_request獲取用戶很容易。) –

相關問題