我需要一些幫助,使用do阻止返回錯誤的生成鏈接。我知道這是一個簡單的修復,但我似乎無法弄清楚,因爲我對於我這麼裸露的rails非常陌生。Rails阻止返回用戶而不是合作伙伴
我正在構建維基百科的應用程序克隆,其中有wiki,用戶和現在的合作者。這個克隆是用於項目教程的,但在視圖中實現協作者的具體步驟讓我感到困惑。通過下面的代碼,我得到了當前用戶的用戶#show上生成的鏈接。 我真正需要的是當前用戶是協作者的維基列表。
讓我知道是否需要其他代碼來幫助解決此問題,並提前感謝您的幫助。
用戶顯示視圖
<div class="col-sm-4">
<div class="col-md-4">
<% @collaborator.each do |collaborator| %>
<div class="row">
<ul>
<li><%= link_to collaborator %></li>
</ul>
</div>
<% end %>
</div>
</div>
用戶控制器
def show
@user = User.find(params[:id])
@wikis = @user.wikis
@wiki = Wiki.find(params[:id])
@collaborators = Collaborator.all
@collaborator = @wiki.collaborators.find_by(:user_id => current_user.id)
end
用戶模型
class User < ActiveRecord::Base
has_many :wikis
has_many :collaborators
has_many :collaborating_wikis, through: :collaborators, foreign_key: "wiki_id", class_name: "Wiki"
validates :wiki_id, presence: true
end
wiki模式
class Wiki < ActiveRecord::Base
belongs_to :user
has_many :collaborators
has_many :users, through: :collaborators
validates :user_id, presence: true
end
合作者型號
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
validates :wiki_id, :user_id, presence: true
validates :wiki_id, uniqueness: { scope: [:wiki_id, :user_id] }
end
嗨塔克,這將是有一個鏈接到GitHub庫真正有用的,所以我可以克隆下來回購,並有一齣戲。 – Andy
你上面的代碼在'@ collaborator'上執行'each'循環,而不是'@ collaborators'。 –