我目前遇到了一个问题,我尝试设置只有创建组的用户可以编辑或删除组的能力。我收到以下错误:未定义的方法`用户为#当前用户在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
再次,我的两个指数,显示页组我得到以下错误:未定义的方法`用户为#谢谢你的任何帮助。
这部分工作 - 错误消息消失了,但即使创建组的用户,他们也不能编辑它们。任何解决方案 –
我需要更多信息来评估问题所在。我感觉这是'has_many_through'的关联问题。从命令行进入'rails console'并且1)'@group = Group.find(your_group_id_that_has_); @ group.user#=>应该返回一个用户权限?那么2)'@group.users#=>应该返回所有相关的用户权限?LMK如果1和2有效 - 我可能会为你提供一个解决方案。 – SoAwesomeMan
没问题,输入group = Group.find(1)我得到的组ID为1.但是,如果我输入group.user 1,我会得到rails console错误消息。这有帮助吗?让我知道你需要什么其他信息! –