林想看看是否有更好的方式,通過documents
遍歷每個skill
贏得我的應用程序是我目前的實現是造成我用ajax訪問值的Rails
我的模型
時發出class Skill < ActiveRecord::Base
has_many :documents
end
class Document < ActiveRecord::Base
belongs_to :skill
belongs_to :user
end
class User < ActiveRecord::Base
has_many :documents
end
在我看來public/index
我顯示所有我Skills
和每個用戶已連接到一個特定Skill
documents
的
查看
<% @skills.group_by(&:year_group_name).each do |key, value| %>
<h3><%= key %></h3>
<% value.group_by(&:aspect_name).each do |key_one, value_one| %>
<h4><%= key_one %></h4>
<% value_one.each do |s| %>
<% document = @documents.select { |d| d.skill_id == s.id } %>
<li><%= s.skill_description %>
<%= render partial: 'shared/documents/document_form', locals: { document: document } %>
</li>
<% end %>
<% end %>
<% end %>
document_form
<% if document %>
<% document.each do |doc| %>
<%= doc %><!-- renders image if exists for user -->
<% end %>
<%= render template: '/documents/new' %>
<% else %>
<%= render template: '/documents/new' %>
<% end %>
當我創建它是通過AJAX做,叫我create.js.erb
create.js.erb文檔
$('.doc_upload').html("<%= j render(partial: 'shared/documents/document_form', locals: { document: @documents }) %>")
CRE在文件控制器吃行動
def create
@document = current_user.documents.new(document_params)
respond_to do |format|
if @document.save
format.html { redirect_to root_path, notice: success_save_msg }
format.js
else
format.html
format.js { render action: 'create.js.erb' }
end
end
末
當我渲染時我的問題create.js.erb
(所以當驗證例如失敗)
你可以看到,我路過當地@documents
,當其我打電話document_form
正在迭代所有用戶文檔,而不僅僅是針對特定技能的文檔。
希望這是有道理的,我會很感激,如果任何人都可以給我任何指針
感謝
對不起,但你能改說你的問題嗎?看來你在1線程下有太多問題,我對你的「真正」問題有點混淆。 – Finks 2015-02-01 01:07:49
@Finks我已經重組了我的問題,希望現在有更多意義,謝謝 – Richlewis 2015-02-01 09:36:04
也許我不能正確理解你的代碼,但是我沒有找到你在#create動作中定義\ @documents變量的位置。只有\ @文件。也許有任何提取所有用戶文檔的before_action? – hedgesky 2015-02-01 11:44:01