Rails:部分中的远程表单:一次工作,而不是两次 [英] Rails: Remote Form in a Partial: works once , not twice

查看:57
本文介绍了Rails:部分中的远程表单:一次工作,而不是两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最后几天遇到了一个问题。
我想使用远程表单更新对象。
我第一次提交表单时基本上可以更新我的对象,但第二次不能正常工作。

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

view/missions/_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..."

这是我的控制器
/ app / controllers / missions_controller。 rb

This is my controller /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

这是/missions/update.js.erb:

This is /missions/update.js.erb:

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

我的对象的更新工作一次,而不是两次。在firebug控制台上,我可以看到我的表单类型是第一次:text / javascript,下次打开text / html ...

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...

感谢您的帮助。

推荐答案

首次在您的网页中新加载javascript。所以它会奏效。
下次你的javascript不工作意味着你可能会在document.ready中添加你的脚本代码.......

For First time the javascript is freshly loading in your page. So it will work. Next time your javascript is not working means you may be added your script code inside document.ready.......

对于这个问题,你有两个解决方案:

For this issue you have two solutions:


  1. 您需要在每次更新时再次调用javascript文件。

  2. 您在特定的javascript函数中指定更新调用,并且必须在页面中的每次更新时调用该javascript函数。

这篇关于Rails:部分中的远程表单:一次工作,而不是两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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