【问题标题】:How to render the new action of another controller with ajax/jquery in rails 3如何在 Rails 3 中使用 ajax/jquery 渲染另一个控制器的新动作
【发布时间】:2012-06-04 21:43:03
【问题描述】:

我想知道如何在 rails 3 中使用 ajax/jquery 渲染另一个控制器的新动作。

假设我有一个客户和一个销售控制器、模型和视图,我想做的是能够使用 ajax/jquery 从客户索引视图进行新的销售。

为此,我在每个名为“New Sale”的客户旁边都有一个按钮,当点击它时,它应该呈现表单以为该客户进行新的销售。

我的想法是,它可以通过从 Sales 控制器渲染 new_sale 表单或渲染在 Clients 视图文件夹中创建的新销售表单并在 Clients 控制器内创建新操作以创建新销售来完成。我不确定这是否可以做到。

无论如何我想知道如何呈现新的销售表格,有什么想法吗?

提前谢谢你。

编辑** 这是我的代码:

#clients index.html.erb
#I couldnt make work the link like you suggested so i did it like this
<%= link_to "Venta", { :controller => "sales", remote: true, :action => "new", :cliente_id => cliente.id, :class => 'btn btn-small btn-primary', :id => 'btn-venta-clientes-index'} %>

#Div where i want to render the new sale form
<div id="test" class="modal-body"></div>


#new.js.erb inside sales views folder, i tried with the 3 of them one at a time but none work
#$("div#test").html("<%= escape_javascript(j render :partial => "form") %>"); 
#$('div#test').html('<%= escape_javascript(render("form")) %>');
$('div#test').html('<%= j render :partial => "form" %>');


#Sales controller
#I tried removing all formats from the new action but didnt work
def new
  @sale = Sale.new
  @cliente = Cliente.find(params[:cliente_id])

  respond_to do |format|
    #format.html # new.html.erb
    #format.json { render json: @sale }
format.js
  end
end

#I tried removing all formats from the create action but didnt work
def create 
  @sale = Sale.new(params[:sale])

  respond_to do |format|
    if @sale.save
     #format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
     #format.json { render json: @sale, status: :created, location: @sale }
 format.js  { render action: "create" }
    else
     #format.html { render action: "new" }
     #format.json { render json: @sale.errors, status: :unprocessable_entity }
 format.js  { render action: "new" }
    end
  end
end

你知道我做错了什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 jquery ruby-on-rails-3.1


    【解决方案1】:

    我用我制定的解决方案大致勾勒了您的情况​​。也许有些地方不是最优的,有些地方是错误的,但我希望你能比文字更好地理解大体的想法:

    #clients/index.haml
    = render @clients
    
    
    #clients/_client.haml
    .client-container
      %p= client.name 
      ...
      .form-container{ :id => "client-#{client.id}" }
        = link_to "New sale", new_sale_path(:client_id => client.id), :remote => true
    
    #SalesController
    def new
      @sale = Sale.new
      @client = Client.find(params[:client_id])
    end
    
    #sales/new.js.erb
    $("#client-<%= @client.id %>").html("<%= j render :partial => "form" %>");
    
    #sales/_form.haml
    #your form code
    
    #SalesController
    def create
      @sale = Sale.new(params[:sale])
      if @sale.save
        format.js { render action: "create"}
      else
        format.js { render action: "new" }
      end
    end
    
    #sales/create.js.erb
    #Here you hide a form and write something like "success".
    #You can return "new sale" button (if you decide to remove it.)
    

    此外,ajax 回调在这种情况下非常有用。例如:

    $('.your-remote-button').bind 'ajax:success', ->
      $(this).closest('div').fadeOut()
    

    【讨论】:

    • 感谢米哈伊尔 D 的快速回复
    • 我是 Rails、Jquery 和 Ajax 的新手,但让我看看你是否明白你在说什么。首先我应该在销售视图文件夹中创建一个 new.js.erb 文件来呈现新的销售表单,然后在销售控制器的创建操作中我只使用 format.js 对吗?我猜想ajax位是关闭渲染的div。我会试一试,然后告诉你进展如何。
    • @user1350443,不客气。是的,像这样。我担心 Rails 有很多魔法,你必须调试这段代码。最重要的是 - 如果遇到问题,请查看 AJAX 请求日志。
    • 好的,我已经尝试使用代码在 div 中呈现新的销售表单但无法正常工作,我一直被重定向到 new_sale_apth 而不是在 div 中呈现表单,我尝试删除格式新销售操作中的 .html 和 format.json 甚至也从新销售操作中删除了 format.js,但是当我这样做时,按钮不会做任何事情。我将编辑我的问题以发布我的代码。
    • @user1350443,您的link_to 错误:url 参数与其他参数混合在一起。试试这个:&lt;%= link_to "Venta", new_sale_path(:cliente_id =&gt; cliente.id), :remote =&gt; true %&gt;apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 2011-06-02
    • 2012-01-31
    • 2014-05-28
    • 1970-01-01
    • 2018-04-18
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多