我一直不断地得到这个恼人的错误,我无法解决它。我最近发布了一个关于同一主题的问题,并没有得到有效的帮助。
我希望用户请求加入群组。 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 :(得分: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)
如果您正在使用资源丰富的路由]。您可以使用rake路线寻求帮助。如果您在development.log或test.log中看到:cliq_id
和:id
等内容作为被点击的网址的一部分,那么应该是数字。您也可以自己插入字符串(例如"/cliqs/#{cliq_id}/cliq_requests/#{cliq_request.id}"
),但这通常会更加打字,而且随着时间的推移肯定会更加脆弱。
您的一个问题可能是您正在遍历待处理成员名称列表,这些成员名称没有正确形成链接所需的所有数据。因此,您的更新操作可能正常,但您可能无法向其传递正确的数据。
这一行:
if @current_user = @cliq.owner
是一个赋值,因此总是返回true。大概你的意思是==