通过Ajax调用和谐音Rails的访问值 [英] Accessing values through ajax calls and partials Rails

查看:98
本文介绍了通过Ajax调用和谐音Rails的访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林想看看是否有更好的方法来遍历文件每个技能赢取我的应用程序作为我目前的实现是使用Ajax时造成我的问题

Im 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

在我看来公共/指数我告诉我所有的技能和每个文件用户已连接到特定技能

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.

我希望这是有道理的,我会AP preciate它,如果任何人都可以给我任何指针

Hopefully this makes sense and i would appreciate it if anyone could give me any pointers

感谢

推荐答案

您的问题是因为你试图使用部分共享/文件/ document_form 。这部分是期待文件变量是一个数组,而不是一个单独的对象。我们可以在你看来,你已经初始化看到文件处理文档的列表中,选择方法返回一个数组根据条件

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.

创建的动作,你已经初始化 @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.

无论是创建另一个部分,可以处理你的预期来看,还是重构现有的部分来处理不同的数据类型文件变量。

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 部分,你调用的局部变量每个方法文档吗?你可以不叫每个 current_user.documents.new(document_params)是的<$ C的值当你打电话的部分通过AJAX $ C>文件变量。

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.

这篇关于通过Ajax调用和谐音Rails的访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆