【发布时间】: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