将局部变量传递给在视图加载后呈现的局部变量 [英] Passing a local variable to a partial that is rendered after the view has already loaded

查看:31
本文介绍了将局部变量传递给在视图加载后呈现的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我刚刚发现了另一个线程,它解释了我遇到这个问题的原因(将本地 rails 变量传递给 javascript 到部分 ),但它没有列出修复它的步骤(或者至少它跳过了太多我没有的步骤还知道怎么做.)问题似乎是传递给局部变量的局部变量不再是加载 new.html.erb 时存在的局部变量,因为局部变量直到点击事件才被加载.有没有可能解决这个问题?谢谢.

Update: I just found this other thread that explains why I am having this issue ( Pass local rails variable to javascript to partial ), but it did not list out the steps to fix it (or at least it skipped too many steps that I don't yet know how to do.) The issue seems to be that the local variable being passed to the partial is no longer the same local variable that existed when new.html.erb was loaded since the partial is not loaded until the click event. Is it possible to get around that? Thanks.

原文:

我有一个链接到一个链接的点击事件,该链接只在某些时候有效,我不知道为什么.当单击链接而不重新加载页面时,它应该将部分渲染到我的视图中.

I have a click event tied to a link that only works some of the time and I am not sure why. It is supposed to render a partial into my view when a link is clicked without reloading the page.

这是我目前拥有的(不起作用):

This is what I currently have (which is not working):

comps_helper.rb:

comps_helper.rb:

def addRound(f)
  render "add_round", f: f
end

comps/new.html.erb

comps/new.html.erb

<div id="add_round"></div>
<%= link_to "Add round", addRoundLink_path, remote: true %>

addRoundLink.js.erb

addRoundLink.js.erb

$("#add_round").html("<%=j addRound(f) %>");

有什么作用:

  1. 如果我直接从 new.html.erb 使用 <%= addRound %> 调用辅助方法,它就可以工作.但是我需要在单击链接时触发它,以便用户可以根据需要向表单添加任意数量的部分.

  1. If I just call the helper method directly from new.html.erb with <%= addRound %>, it works. But I need it to be triggered when the link is clicked so the user can add as many sections to the form as they'd like.

如果上面的代码中没有包含 'f' 参数,那么 javascript 就可以在点击时工作.例如,如果我更改部分,使其仅包含一串文本,则单击链接时它将呈现该文本.这让我觉得我如何在 javascript 中包含 'f' 参数存在问题.但我不知道为什么会这样,因为 javascript 不是只是将该代码插入到 new.html.erb 中,在那里我有带有add_round"ID 的 div?

If the 'f' parameter is not included in the code above, the javascript does work on click. For example, if I change the partial so it just includes a string of text, it will render that text when the link is clicked. This makes me think there is an issue with how I included the 'f' parameter in the javascript. But I'm not sure why that is the case since isn't the javascript just inserting that code back into new.html.erb where I have the div with the "add_round" id?

注意:这是我单击链接时在 javascript 控制台中看到的内容:GET http//0.0.0.0:3000/addRoundLink 500(内部服务器错误)
jquery.js:8215

Note: This is what I see in my javascript console when I click the link: GET http //0.0.0.0:3000/addRoundLink 500 (Internal Server Error)
jquery.js:8215

谢谢.

推荐答案

它不起作用,因为 f 仅存在于您的控制器操作的上下文中.

It's not working because f only exists in the context of your controller action.

当用户点击您的链接时,会为您的 javascript 发出一个新的 HTTP 请求.处理此请求的控制器动作与呈现页面的控制器动作不同,因此您需要在此控制器动作中设置 f 并将其作为本地传递给 addRoundLink.js.erb.

When a user clicks your link, a new HTTP request is made for your javascript. The controller action handling this request is different from the one that rendered the page, so you need to set f in this controller action and pass it as a local to addRoundLink.js.erb.

示例代码

  • 我假设 f 由您的模型对象/实例决定.
  • I'm assuming f is determine by your model object/instance.

在你的控制器中

   def add_round
        model = MyModel.find params[:id]
        f = model.determine_f

        render :partial => 'add_round', :locals => { :f => f }
   end

在你的 new.html.erb

In your new.html.erb

<%= link_to "Add Round", add_round_path(@model), :remote => true %>

这篇关于将局部变量传递给在视图加载后呈现的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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