简体   繁体   English

Rails 3,远程表单重定向,想留在页面上,缺少模板错误?

[英]Rails 3, remote form redirecting, want to stay on page, missing template error?

I think I must be doing something stupid. 我想我一定是在做一些愚蠢的事。 Unfortunately, due to the nature of stupidity, I can't tell what I'm being stupid about. 不幸的是,由于愚蠢的性质,我无法分辨出我愚蠢的是什么。 So, as you can guess, I feel pretty stupid. 所以,你可以猜到,我觉得很愚蠢。

Please help me surmount this brick wall of stupidity. 请帮我克服这个愚蠢的砖墙。

I want to be able to click on a submit button and not leave the page, moreover, for that page to not even attempt to reload! 我希望能够点击提交按钮,而不是离开页面,而且,该页面甚至没有尝试重新加载! This is accomplished by ajax and I've read several guides on it, I have even had it working on another project before. 这是由ajax完成的,我已经阅读了几个指南,我甚至让它在之前的另一个项目上工作。 In this instance I'm hiding the div with scriptaculous. 在这个例子中,我用scriptaculous隐藏了div。 In the previous version of ajax form submission I used a (pseudo code) 在之前版本的ajax表单提交中我使用了(伪代码)

   render update do |page| 
       page replace html with this div 
   end

But this time I'm not actually going to replace the div or anything, I'm just hiding it with scriptaculous with an onclick event.... 但是这次我实际上并没有替换div或者任何东西,我只是用一个onclick事件的scriptaculous隐藏它....

Alls I want is for the form to actually submit and for it to disappear. 我想要的就是让表格实际提交并让它消失。

So I'm working on a form, here's the form tag: 所以我正在处理一个表单,这是表单标签:

        <%=form_for :call_back_request, :remote => true, :url => {:action => "call_back_request_cbrsent"} do |c| %>

As you can see it is :remote => true 你可以看到它是:remote => true

I have various form entry fields, which I think are more or less irrelevant, here's the relevant parts: 我有各种表单输入字段,我认为或多或少不相关,这里是相关部分:

This is a field that is part of the submit button, so I've included it here: 这是一个提交按钮的一部分,所以我在这里包含它:

    <%= hidden_field_tag :instructor_id, "empty", :id=>"instructor_hidden_field" %>

this is the submit button, as you can see I'm using scriptaculous to hide a div, that's the div the the submit button is in. 这是提交按钮,你可以看到我正在使用scriptaculous来隐藏div,这就是提交按钮所在的div。

It could be affecting this so rather than delete that from this stackoverflow question I've included it. 它可能会影响这一点,而不是从这个stackoverflow问题中删除它我已经包含它。

    <%=c.submit 
    :value=>"SEND", 
    :style=>"width:90px; height:33px; padding-bottom:0; font-size:1em;", 
    :class=>"lpl-go-button lpl-go-button-submit", 
    :onclick=>"Effect.BlindUp('cbr_row_#{rp[:name]}');  document.getElementById('instructor_id').value = '#{rp[:instructor_id]}';return false;", :remote=>true %>

(line breaks added by me here to prevent you guys having to scroll right so much) (我在这里添加了换行符,以防止你们不得不向右滚动)

From the controller : 从控制器:

      def call_back_request_cbrsent
        @call_back_request = CallBackRequest.new(params[:call_back_request])
        @instructor = Instructor.find_by_id(params[:instructor_id])
        @call_back_request.instructor_id = params[:instructor_id]
        @call_back_request.save
        email = UserMailer.create_send_call_back_request(@call_back_request, @instructor)
        email.set_content_type("text/html")
        UserMailer.deliver(email)
      end

Now, the thing is, as far as my tired mind can see the return false; 现在,问题是,只要我疲惫的头脑可以看到回报是虚假的; & the remote => true (in the submit tag specifically) do nothing much. &remote => true(具体在提交标签中)什么都不做。 My brain's cache is totally chock a block, I am having trouble thinking. 我的大脑缓存完全阻塞,我无法思考。 Feel like I need to take a step back and stuff cause I'm not seeing the problems here. 感觉我需要退后一步,因为我没有看到这里的问题。

Thanks to anyone who takes the effort to get through my ramblings here to give me an answer, I appreciate that I've got a lot of waffle up there. 感谢任何努力通过我的ramblings在这里给我一个答案的人,我感谢我在那里有很多华夫饼干。 Really sorry. 真对不起。 Dead tired. 精疲力竭。 Think Dr. Jonestone said "sorry I didn't have time to write a short one" - bit of that going on here. 想想Jonestone博士说:“对不起,我没有时间写一篇简短的文章” - 其中有一点是在这里发生的。

Just to clarify, currently I'm getting missing template error on a remote form... I tried adding this 只是为了澄清,目前我在远程表单上丢失了模板错误...我试着添加它

    render :nothing => true

But, it iss not working. 但是,它不起作用。

It does a good job at rendering a blank page, I don't know what the point in this would be so I am assuming that either it is not working properly for me or it's not appropriate in this context for some reason. 它在渲染空白页面方面做得很好,我不知道这个问题的重点是什么,所以我假设它对我来说不正常,或者由于某些原因它在这种情况下不合适。

Try (in your controller) 尝试(在您的控制器中)

render :nothing => true

Edit: 编辑:

Ah, if you had a template error you should've said so. 啊,如果你有模板错误,你应该这么说。

I just noticed that you want to remove the div also. 我只是注意到你想要删除div。

So what you want to do, it in your controller: 那么你想要做什么,它在你的控制器中:

def call_back_request_cbrsent
....
    respond_to do |format|
      format.html
      format.js
    end
end

And then make a call_back_request_cbrsent.js.erb file in your views. 然后在视图中创建一个call_back_request_cbrsent.js.erb文件。 This will contain the javascript to remove that form. 这将包含用于删除该表单的JavaScript。 It obviously contain something like the following: 它显然包含如下内容:

$("#form").remove()

...That's in jQuery, but just use whatever javascript you've been using to remove the form. ...这是在jQuery中,但只是使用你一直用来删除表单的任何javascript。

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

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