2016-06-28 58 views
0

我目前遇到了一个问题,我尝试设置只有创建组的用户可以编辑或删除组的能力。我收到以下错误:未定义的方法`用户为#当前用户在rails中编辑/删除的问题4

我在我的群折射率控制器下面的代码:

<% @nearby_groups.each do |group| %> 
    <h1><%= group.topic %></h1> 
    <p><%= group.description %></p> 
    <% if current_user.id == group.user.id %> 
    <%= link_to "Edit", edit_group_path(group), class: "ui black button" %> 
    <%= link_to "Delete", group_path(group), method: :delete, data: { confirm: "Are you Sure?"}, 
    class: "ui inverted red button " %> 
    <% end %> 
<%= link_to "show", group_path(group), class: "ui inverted blue button" %> 
<% end %> 

在我的群展的网页我有下面的代码:

<h1>The <%= @group.topic %> Group!</h1> 
    <p>Description: <em><%= @group.description %></em></p> 
    <p>Group Address: <em><%= @group.address %></em></p> 
    <p>Group City: <em><%= @group.city %></em></p> 
    <p>Group State: <em><%= @group.state %></em></p> 
    <% if current_user.id == @group.user.id %> 
    <%= link_to "Edit", edit_group_path(), class: "ui black button" %> 
    <%= link_to "Delete", group_path(), method: :delete, data: { confirm: "Are you Sure?"}, 
    class: "ui inverted red button " %> 
<% end %> 

我的组控制器看起来是这样的:

def index 
    @groups = Group.includes(:comments) 
    end 

    def show 
    @group = Group.find(params[:id]) 
    end 

最后,我的组模型厕所KS这样的:

class Group < ActiveRecord::Base 
    has_many :collections 
    has_many :comments 
    has_many :users, :through => :collections 

    validates :topic, presence: true 
    validates :description, presence: true, length: { minimum: 10 } 
    validates :address, presence: true 
    validates :city, presence: true 
    validates :address, presence: true 

    geocoded_by :address 
    after_validation :geocode 
end 

再次,我的两个指数,显示页组我得到以下错误:未定义的方法`用户为#谢谢你的任何帮助。

回答

0

变化:

<% if current_user.id == @group.user.id %> 

要:

<% if current_user.id == @group.try(:user).try(:id) %> 

还是占current_usernil当用户没有登出:

<% if current_user and current_user.id == @group.try(:user).try(:id) %> 

参考:

# +try+ calls can be chained: 
# 
# @person.try(:spouse).try(:name) 

通过:https://github.com/rails/rails/blob/be589a8b01500a4b52a86248458bf57597e568e8/activesupport/lib/active_support/core_ext/object/try.rb#L16


注:这个答案并不占Active Record的关联问题:http://guides.rubyonrails.org/association_basics.html#the-types-of-associations


更新

根据您的has_many_through屁股ociation,复数Group#users应该返回一个Active Record关系对象,它看起来像一个包含任何关联用户的数组。

更改为:如果组所属

# app/models/users.rb 
class User 
    def has_group_access?(group_id) 
    Collection.find_by_group_id_and_user_id(group_id, self.id) 
    end 
end 

# app/views/whatevs.html.erb 
<% if current_user and current_user.has_group_access?(@group.id) %> 

更新2

<% if current_user and @group.users.map {|user| user.id}.include?(current_user.id) %> 

这种类型的逻辑应移到模型层,例如给用户,然后更改为:

<% if current_user and current_user.id == @group.user_id %> 
+0

这部分工作 - 错误消息消失了,但即使创建组的用户,他们也不能编辑它们。任何解决方案 –

+0

我需要更多信息来评估问题所在。我感觉这是'has_many_through'的关联问题。从命令行进入'rails console'并且1)'@group = Group.find(your_group_id_that_has_); @ group.user#=>应该返回一个用户权限?那么2)'@group.users#=>应该返回所有相关的用户权限?LMK如果1和2有效 - 我可能会为你提供一个解决方案。 – SoAwesomeMan

+0

没问题,输入group = Group.find(1)我得到的组ID为1.但是,如果我输入group.user 1,我会得到rails console错误消息。这有帮助吗?让我知道你需要什么其他信息! –