使用RJS替换div

发布于 2024-07-23 18:00:08 字数 805 浏览 8 评论 0原文

我目前有一个链接,单击该链接会调用一个 JavaScript 函数,该函数会收集一些数据,然后使用 ProtoType 提交表单...

function myFunction(){
  data = someobject.getData();
  $('myform').request({
    parameters: {data:data, id:id,},
    onSuccess: function(transport) { 
      document.location.reload();        
    }
  });  
}
...
<%= form_tag({:controller => "data", :action => "process"}, :id => "myform") %></form>

请注意我的非 AJAXy document.location.reload(); 在 onSuccess 回调中。

我希望 DataController#process 执行此操作...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

任何人都可以指导我在调用 JavaScript 时需要更改哪些内容才能使 page.replace 工作吗? 当然,现在它只是被忽略了。

我尝试了各种方法,并四处阅读,但在这种情况下我没有得到我需要做的事情。

谢谢

I currently have a link which when clicked calls a JavaScript function, which gathers some data and then uses ProtoType to submit a form...

function myFunction(){
  data = someobject.getData();
  $('myform').request({
    parameters: {data:data, id:id,},
    onSuccess: function(transport) { 
      document.location.reload();        
    }
  });  
}
...
<%= form_tag({:controller => "data", :action => "process"}, :id => "myform") %></form>

Notice my very unAJAXy document.location.reload(); in the onSuccess callback.

I want DataController#process to do this...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

Can anyone guide me as to what I need to change in the calling JavaScript for page.replace to work? Right now of course, it just gets ignored.

I've tried various things, and read around, but I'm not getting what I need to do in this situation.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-07-30 18:00:08

将渲染从控制器中取出并将其放入views/datas/process.js.rjs

page.replace("my_div", :partial => "test")

Take the rendering out of the controller and put this in views/datas/process.js.rjs

page.replace("my_div", :partial => "test")
百变从容 2024-07-30 18:00:08

您可以将 form_for 更改为:

<% form_remote_tag :url => {:controller => "data", :action => "process"},
                   :html => {:id => 'my_form'},
                   :update => 'my_form' do %>

然后在控制器中渲染部分,它将用它更新“my_form”:

def process
  ...
  render partial => 'test'
end

You could change your form_for to this:

<% form_remote_tag :url => {:controller => "data", :action => "process"},
                   :html => {:id => 'my_form'},
                   :update => 'my_form' do %>

And then render the partial in the controller and it will update 'my_form' with it:

def process
  ...
  render partial => 'test'
end
带刺的爱情 2024-07-30 18:00:08

我不确定你到底希望它如何工作,但有一个 remote_form_for 标签将为您执行请求。 另外,您可能希望查看下面的链接,了解使用 RJS 模板的示例:

http ://www.developer.com/lang/article.php/3668331

I'm not sure exactly how you want it to work, but there is a remote_form_for tag that will do the request for you. Also, you may wish to look at the link below for an example on using RJS templates:

http://www.developer.com/lang/article.php/3668331

你怎么这么可爱啊 2024-07-30 18:00:08

感谢您的所有建议 - 这就是我最终所做的:

function myFunction(){
  ...
  $('data').value = data;
  $('id').value = id;
  $('myform').request();  
}
...
<%= form_remote_tag 
  :url => {:controller => 'data', :action => 'process' }, 
  :html => {:id => 'myform'}%>
     <input type="hidden" id="data" name="data"/>
     <input type="hidden" id="id" name="id"/>
</form>

在 DataController#process 中执行此操作...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

(章节 - 我会将其部分放置!)

Thanks for all suggestions - here's what I ended up doing:

function myFunction(){
  ...
  $('data').value = data;
  $('id').value = id;
  $('myform').request();  
}
...
<%= form_remote_tag 
  :url => {:controller => 'data', :action => 'process' }, 
  :html => {:id => 'myform'}%>
     <input type="hidden" id="data" name="data"/>
     <input type="hidden" id="id" name="id"/>
</form>

In DataController#process to do this...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

(chap - I'll put this in partial!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文