我有一个表单可以创建新产品/views/quotes/edit.html.erb (在这里以这种形式编辑引用,这就是为什么它在引用目录中)
<%= form_for (@product), :remote => true do |p| %>
<p>
<%= p.label :product_part %>
<%= p.text_field :product_part%>
</p>
<p>
<%= p.submit %>
</p>
<% end %>
这是我的表/views/quotes/edit.html.erb
<table id="products">
<tr>
<th>Product/Part</th>
<th>Unit/Mdl</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
<tbody>
<%= render @products %>
</tbody>
</table>
这是我的控制器/controllers/products_controller.rb
中的创建定义 def create
@quote = Quote.find(session[:edit_quote])
@product = @quote.products.create!(params.require(:product).permit(:product_part))
respond_to do |format|
format.html {redirect_to customers_url}
format.js {}
end
end
以下是我的部分内容适用于页面刷新/views/products/_product.html.erb
<tr>
<td><%= product.product_part %></td>
<td><%= product.unit_mdl %></td>
<td><%= product.description %></td>
<td><%= product.quantity %></td>
<td><%= product.price %></td>
<td><%= product.total %></td>
</tr>
这是我的create.js.erb文件,用于使用服务器的响应呈现部分。 /views/products/create.js.erb
$('#products').append('<%= render @product %>');
当我提交表单时,表单会正确地将数据保存到Product表,但create.js.erb文件不会更新表。如果我替换&lt;%= render @product%&gt;使用硬编码的html,然后硬编码值正确附加到我的表。我也没有从终端得到任何错误。这是终端输出
Started POST "/products" for 10.0.2.2 at 2015-12-19 17:11:05 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ProductsController#create as JS
Parameters: {"utf8"=>"✓", "product"=>{"product_part"=>"sample product"}, "commit"=>"Create Product"}
Quote Load (6.2ms) SELECT "quotes".* FROM "quotes" WHERE "quotes"."id" = ? LIMIT 1 [["id", 1]]
(0.2ms) begin transaction
SQL (25.1ms) INSERT INTO "products" ("product_part", "quote_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["product_part", "sample product"], ["quote_id", 1], ["created_at", "2015-12-19 17:11:05.587549"], ["updated_at", "2015-12-19 17:11:05.587549"]]
(8.8ms) commit transaction
Rendered products/_product.html.erb (0.2ms)
Rendered products/create.js.erb (107.3ms)
Completed 200 OK in 499ms (Views: 414.0ms | ActiveRecord: 40.3ms)
任何帮助将不胜感激!提前致谢
答案 0 :(得分:0)
我用
替换了我的create.js.erb的内容$("<%= escape_javascript(render @product) %>").appendTo("#products");
这解决了我的问题。我仍然不确定为什么我的其他解决方案不起作用,但这个确实无疑。
答案 1 :(得分:0)
你可以试试这个
$('table#product tbody').append("<%= j render(@product) %>")