如何将 onclick 事件附加到 link_to_function
以便单击该事件刷新页面上的元素(使用部分)?
当用户单击生成的链接时,我想刷新包含代码的部分,以便更新 i
。
def add_step_link(form_builder)
logger.info 'ADD_STEP_LINK'
link_to_function 'add a step' do |page|
form_builder.fields_for :steps, Step.new, :child_index => 'NEW_RECORD' do |f|
logger.info 'inserted js'
html = render(:partial => 'step', :locals => { :step_form => f, :i=>@i+=1})
page << "$('steps').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });"
end
end
end
我有一个更大的部分,其中仅包含:
<%= add_step_link(technique_form) %>
我正在尝试跟踪技术中的步骤数。 在我创建的表单中,用户可以向一组指令添加新步骤。 现在,我有步骤 1-7 的默认字段。 添加一个步骤,得到步骤 8。问题是后续步骤也编号为“8”。
我正在 http://railsforum.com 中扩展“动态形式的多个子模型”教程/viewtopic.php?id=28447 出于我自己的目的。
How do I attach an onclick event to a link_to_function
such that clicking on the event refreshes an element on the page (using partials)?
When the user clicks the generated link, I'd like to refresh the partial containing the code so that i
gets updated.
def add_step_link(form_builder)
logger.info 'ADD_STEP_LINK'
link_to_function 'add a step' do |page|
form_builder.fields_for :steps, Step.new, :child_index => 'NEW_RECORD' do |f|
logger.info 'inserted js'
html = render(:partial => 'step', :locals => { :step_form => f, :i=>@i+=1})
page << "$('steps').insert({ bottom: '#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime()) });"
end
end
end
I have a partial in a larger form that contains just:
<%= add_step_link(technique_form) %>
I am attempting keeping track of the number of steps in a technique. In the form I am creating, users can add new steps to a set of instructions. Right now, I have default fields for steps 1-7. Adding one step, gets you step 8. The problem is that subsequent steps are numbered '8' also.
I am extending the "Multiple child models in a dynamic form" tutorial in http://railsforum.com/viewtopic.php?id=28447 for my own purposes.
发布评论
评论(2)
好吧,我对你在做什么有点困惑,但我会尝试提出一些建议。
不要使用link_to_function,而是使用link_to_remote。 链接到函数调用 javascript,但您实际想要做的是回调到控制器,然后运行一些 rjs 以替换完整的部分,或者更有可能将新的部分(包含步骤)附加到当前的末尾脚步。
这与您将看到的所有这些示例类似,人们在博客评论末尾添加评论(期望使用 link_to_remote 而不是 remote_form_for),看到 http://media.rubyonrails.org/video/rails_blog_2.mov
您可以在此处查看 link_to_remote 的文档:http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
Ok, I'm a little confused about what you are doing, but I'm going to try and make some suggestions.
Rather than use link_to_function, use link_to_remote. Link to function calls javascript, but what you actually want to do is call back to a controller that then runs some rjs to either replace the full partial or, more likely, append the new partial (containing the step) to the end of your current steps.
This is similar to all those examples you will have seen where people append a comment to the end of their blog comments (expect using link_to_remote rather than remote_form_for) see 3/4 of the way through http://media.rubyonrails.org/video/rails_blog_2.mov
You can see the docs for link_to_remote here: http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
我建议按照 RichH 的建议使用
link_to_remote
。 对你来说,诀窍似乎是跟踪@i
。 我要么将其作为 URL 参数放入link_to_remote
中,要么放入会话对象中。 我建议召开会议——看起来更容易。I suggest using
link_to_remote
as RichH suggests. The trick for you it seems is keeping track of@i
. I'd either put it as a URL parameter inlink_to_remote
, or in the session object. I'd suggest the session—it seems easier.