通过Rails中的Ajax调用和局部访问值 [英] Accessing values through ajax calls and partials in Rails
问题描述
我正在寻找是否有更好的方法可以为每个 skill
中的每个 skill
遍历 documents
,因为我当前的实现使我在使用Ajax时遇到问题
I'm looking to see if there is a better way to iterate through documents
for each skill
win my app as my current implementation is causing me issues when using ajax
我的模特
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
,我显示了我所有的技能
和用户已附加到特定的每个
documents
>技能
Within my view public/index
I show all my Skills
and each of the documents
that a user has attached to a particular Skill
class PublicController < ApplicationController
def index
@skills = Skill.all
@documents = current_user.documents.where(skill_id: @skills.map(&:id)).all
end
end
查看
<% @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
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
When I create a document it is done via ajax and calls my create.js.erb
create.js.erb
create.js.erb
$('.doc_upload').html("<%= j render(partial: 'shared/documents/document_form', locals: { document: @documents }) %>")
在文档控制器中创建动作
create action in documents controller
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
时出现问题(例如,当验证失败时)
My problem occurs when I render the create.js.erb
(so when validation fails for example)
您可以看到我正在传递本地 @documents
,当我调用 document_form
时,它正在遍历所有用户文档,而不只是用于特殊技能.
You can see that I am passing the local @documents
, which when i call document_form
is iterating through all of the users documents, as opposed to just the documents for the particular skill.
推荐答案
发生问题是因为您试图使用部分 shared/documents/document_form
.那部分人期望 document
变量是数组而不是单个对象.我们认为,您已经使用文档列表初始化了 document
, select
方法会根据条件返回一个数组.
Your problem happens because you're trying to use the partial shared/documents/document_form
. That partial is expecting the document
variable to be an array instead of a single object. We can see in your view that you've initialized document
with a list of documents, the select
method returns an array based on the condition.
在 create
操作中,您已经将 @document
初始化为Document对象,因此,当您使用 @document
作为 document
在该部分视图中,该部分视图将不会呈现您的预期结果.
In the create
action, you've initialized @document
as a Document object, hence when you used @document
as your value for document
in that partial, the partial view will not render your expected results.
要么创建另一个可以处理您期望的视图的部分,要么重构您当前的部分以处理 document
变量的不同数据类型.
Either create another partial that can handle your expected view, or refactor your current partial to handle different data types of document
variable.
因此,基本上,在 document_form
部分中,您要在局部变量 document
上调用 each
方法,对吗?您不能在 current_user.documents.new(document_params)
上调用 each
,这是通过以下方式调用局部代码时 document
变量的值阿贾克斯.
So basically, in document_form
partial, you're calling each
method on the local variable document
right? You can't call each
on current_user.documents.new(document_params)
which is the value of the document
variable when you called the partial through ajax.
这篇关于通过Rails中的Ajax调用和局部访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!