这是我在StackOverflow上的第一篇文章,我对编码总体上比较新,所以如果我不小心违反了礼仪,我会提前道歉。
我正在尝试将协作者删除到已由所有者添加的Wiki,但现在他想要删除他们的权限。
这是在我的wiki#edit视图中完成的:
<div class="row">
<%= render partial: 'wikis/form' %>
<br>
<%= render partial: 'collaborators/collaborator'%>
<h3> Collaborators: </h3>
<% @wiki.users.each do |c| %>
<li><%= c.username %></li>
<% if @wiki.user == current_user && current_user.premium? %>
<%= link_to "", wiki_collaborator_path(@wiki, @collaborators), method: :delete, remote: true, class: "glyphicon glyphicon-remove" %>
<% end %>
<% end %>
wiki / _form partial非常标准:
<div class="col-md-8">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter wiki title" %>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter wiki body" %>
</div>
<% if current_user.admin? || current_user.premium? %>
<div class="form-group">
<%= f.label :private, class: 'checkbox' do %>
<%= f.check_box :private %> Private wiki
<% end %>
</div>
<% end %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-primary' %>
</div>
<% end %>
</div>
并且协作者/ _collaborator部分创建了初始协作者:
<% if @wiki.user == current_user && current_user.premium? %>
<%= form_for [@wiki, Collaborator.new] do |f| %>
<%= email_field_tag :collaborator %>
<%= f.submit "Add collaborator", class: 'btn btn-primary' %>
<% end %>
<% end %>
这是Collaborator控制器:
before_action :set_wiki
def create
@collaborator = User.find_by_email(params[:collaborator]) #pulled from email tag in collaborator form_for partial
Collaborator.create(user_id: @collaborator.id, wiki_id: @wiki.id)
if @collaborator.save
flash[:notice] = "#{@collaborator.username} at #{@collaborator.email} has been added as a collaborator to #{@wiki.title}"
else
flash[:error] = "Adding of collaborator failed"
end
redirect_to @wiki
end
def delete
@collaborator = @wiki.users.find(params[:id])
if @collaborator.destroy
flash[:notice] = "#{@collaborator.username} at #{@collaborator.email} has been removed from wiki: #{@wiki.title}"
else
flash[:error] = "Removal of collaborator failed"
end
redirect_to @wiki
end
private
def set_wiki
@wiki = Wiki.find(params[:wiki_id])
end
end
Collaborator模型只属于User和Wiki,而Wiki模型如下所示:
class Wiki < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
#Final refactoring eliminates need for delegate method
has_many :collaborators, dependent: :destroy
has_many :users, through: :collaborators
scope :visible_to, -> (user) { user ? all : where(private: false) }
validates :title, length: { minimum: 5 }, presence: true
validates :body, length: { minimum: 20 }, presence: true
end
我从本地服务器返回的错误是
No route matches {:action=>"destroy", :controller=>"collaborators", :id=>nil, :wiki_id=>"104"} missing required keys: [:id]
我到处寻找解决这个问题的方法,但是我在理解has_many的嵌套路由时遇到了一些麻烦:通过关联,当我寻找答案时,似乎每个人都写了不同于我的方法调用。除了这个link_to方法之外,我的应用程序中的所有功能都有功能,所以我宁愿不重写它只是为了匹配别人的代码,除非我真的搞砸了。
我尝试过@collaborator.id
以及其他一些事情只是为了试图强行解决方案,但没有任何工作,我不知道我在这里寻找什么。
任何帮助都将非常感谢。谢谢^^
答案 0 :(得分:0)
No route matches {:action=>"destroy", :controller=>"collaborators", :id=>nil, :wiki_id=>"104"} missing required keys: [:id]
基本问题在于:
<%= link_to "", wiki_collaborator_path(@wiki, @collaborators), method: :delete, remote: true, class: "glyphicon glyphicon-remove" %>
该错误表明您的@collaborators
var未通过单数id
传递给link_to
方法。这是因为我假设@collaborators
是一个集合。
-
您需要传递一个collaborator
对象:
<%= link_to "", wiki_collaborator_path(@wiki, c) ...
由于您使用.each
循环@wiki.users
,因此每个user
(我认为是collaborator
)的local variable
{已分配{1}}:
c
由于您是新用户,我强烈建议您使用font-awesome-rails
而不是<% @wiki.users.each do |c| %>
。主要好处是fa_icon
帮助程序,允许您使用:
glyphicon
答案 1 :(得分:0)
尝试遍历@wiki
的协作者以获取正确的链接。维基的合作者是wiki的用户,因为他们通过协作者关系连接。所以你只是这样做:
<% @wiki.users.each do |collaborator| %>
<%= link_to "", wiki_collaborator_path(@wiki, collaborator), method: :delete, remote: true, class: "glyphicon glyphicon-remove" %>
<% end %>