简体   繁体   English

Rails:部分中的远程表单:一次工作,而不是两次

[英]Rails: Remote Form in a Partial: works once , not twice

I've been confronted to a problem this last days. 我最近几天遇到了一个问题。 I want to update an object using a remote form. 我想使用远程表单更新对象。 I can basically update my object when I submit my form the first time, but it doesn't work the second time. 我第一次提交表单时基本上可以更新我的对象,但第二次不起作用。

So, I have a remote form in a partial. 所以,我有一个偏远的形式。

view/missions/_table_form.haml.erb 视图/任务/ _table_form.haml.erb

%tr{:class => "tr_mission_#{mission.id} tr_mission"}
  = form_for(mission, :url => mission_path(mission), :html => { :remote => true , :method=> :put, :format => :js, :multipart => true, :class => "my_remote_form" }) do |f|
    = f.text_area :description,  :size => "220x6", :class => "fill_data"
    = f.submit  'Update', :class => "btn btn btn-inverse", :disable_with => "updating..."

This is my controller /app/controllers/missions_controller.rb 这是我的控制器/app/controllers/missions_controller.rb

def update
    @mission = Mission.find(params[:id])
    respond_to do |format|
        format.html {
           render :action => "edit"
        }
        format.js {}
    end
  end

This is /missions/update.js.erb: 这是/missions/update.js.erb:

$('.tr_mission_<%= @mission.id %>').replaceWith('<%= escape_javascript( render :partial => "table_form", :locals => {:mission => @mission}).html_safe  %>');

The update of my object works once, not twice, On the firebug console, I can see that the type of my form is the first time: text/javascript and the next time it turns on text/html... 我的对象的更新工作一次,而不是两次,在firebug控制台上,我可以看到我的表单的类型是第一次:text / javascript,下次打开text / html ...

Thank you for your helps. 谢谢你的帮助。

For First time the javascript is freshly loading in your page. 首次,javascript在您的页面中刚刚加载。 So it will work. 所以它会奏效。 Next time your javascript is not working means you may be added your script code inside document.ready....... 下次你的javascript不工作意味着你可能会在document.ready .......中添加你的脚本代码。

For this issue you have two solutions: 对于这个问题,您有两个解决方案:

  1. You need to call javascript files once again on each update time. 您需要在每个更新时间再次调用javascript文件。
  2. You Specify your update calls in particular javascript function and you must call that javascript function on each update fires in your page. 您在特定的javascript函数中指定更新调用,并且必须在页面中的每次更新时调用该javascript函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM