【问题标题】:Ruby on Rails Save Using AJAXRuby on Rails 使用 AJAX 保存
【发布时间】:2012-12-13 20:17:04
【问题描述】:

在我的 Rails 应用程序中,我正在寻找一种保存帖子的方法,并提出“已保存”的通知。无需重定向到任何地方。

我可以在控制器中执行此操作还是必须使用 Ajax?如果我必须使用 Ajax,有没有简单的方法来实现它?

以下是我的控制器中的创建和更新操作:

def create
  @document = current_user.documents.build(params[:document])

  if @document.save
    redirect_to @document.edit, notice: 'Saved'
  else
    render action: "new"
  end
end

def update
  @document = current_user.documents.find_by_url_id(params[:id])

  if @document.update_attributes(params[:document])
    redirect_to @document, notice: 'Saved'
  else
    render action: "edit"
  end
end

【问题讨论】:

    标签: javascript ruby-on-rails ruby ajax


    【解决方案1】:

    您似乎正在尝试将“新文档”表单替换为“编辑文档”表单,并显示已保存的警报。

    说实话,可能最简单的方法是使用 jQuery 将整个表单替换为部分表单。它不是最纤薄的,有办法做所有的 Javascript 客户端,但这会让你开始。

    假设您有一个名为 _new.html.erb 的部分“新文档”表单,请创建一个名为 create.js.erb 的文件并将其放入其中:

    $edit_form = "<%= escape_javascript(render :partial => 'new', :locals => {document: @document}) %>"
    $("#document_form_container").html($edit_form)
    

    然后确保您的表单在form_for 标记中有:remote =&gt; true

    【讨论】:

    • 是这样吗?我有_form.html.erbnew.html.erb。我目前正在使用 jQuery,所以使用它会很好。我需要用自己的 ID 替换 #document_form_container 吗?
    • 是的,在你的渲染中用form替换new:partial。
    • 控制器中的新动作?我该怎么做?抱歉,我是 Rails 新手
    • 不,你的部分被称为 _form.html.erb 所以你想渲染它而不是新的。 render :partial =&gt; 'form', :locals =&gt; {document: @document}
    【解决方案2】:

    如果您不想创建新的控制器操作(而且您可能不应该),那么我建议您将创建和更新操作设置为如下所示:

    def create
      @document = current_user.documents.build(params[:document])
    
      if @flag = @document.save
        respond_to do |format|
          format.html
          format.js
        end
      else
        render action: "new"
      end
    end
    
    def update
      @document = current_user.documents.find_by_url_id(params[:id])
    
      if @flag = @document.update_attributes(params[:document])
        respond_to do |format|
          format.html
          format.js
        end
      else
        render action: "edit"
      end
    end
    

    然后在 app/views/documents/create.js.erb:

    var results_html;
    var status;
    
    <% if @flag %>
      results_html = $('<%= j(render("document"))%>');
      status = "success";
    <% else %>
      results_html = $('');
      status = "failure";
    <% end %>
    
    $('destination').append(results_html); //assuming you're inserting a list item or some content besides the alert onto the page
    
    alert(status); // replace this with your actual alert code
    

    在 update.js.erb 中:

    var results_html;
    var status;
    
    <% if @flag %>
      results_html = $('<%= j(render("document"))%>');
      status = "success";
    <% else %>
      results_html = $('');
      status = "failure";
    <% end %>
    
    $('#<%= @document.id %>').replaceWith(results_html); //assuming you're replacing a list item or some content besides the alert onto the page.  also, make sure you're searching for the correct element id
    
    alert(status); // replace this with your actual alert code
    

    希望这会有所帮助。关键是 rails 允许您为控制器操作上的不同访问方法定义不同的模板。当您发出 AJAX 请求时,您将默认获得 js.erb 视图,这将允许您通过返回将在服务器返回时运行的 javascript 来操作当前页面。

    【讨论】:

    • 在_form.html.erb上添加remote true怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2020-11-28
    • 2020-06-24
    相关资源
    最近更新 更多