我有模型:想法、项目、产品。 我正在尝试在 Idea 的编辑 View 中通过 Items 将 Products 添加到 Ideas。 我的 edit.html.erb - 想法
<div id="items">
<%= render @idea.items %>
</div>
<div class="products">
<% @products.each do |p| %>
<%= p.title %><%= button_to '+', items_path(product_id: p.id, idea_id: @idea.id), remote: true %>
<% end %>
</div>
我的项目 Controller :
def create
product = Product.friendly.find(params[:product_id])
@item = @idea.add_product(product.id)
respond_to do |format|
if @item.save
format.js
end
end
end
想法.rb
def add_product(product_id)
item = items.find_by(product_id: product_id)
if item
else
item = items.build(product_id: product_id)
end
item
end
我的“create.js.erb”
$('#items').html("<%= escape_javascript render(@idea.items) %>");
当我将“format.html {redirect_to :back}”放入 def create (items_controller) 时一切正常,但没有 AJAX=(
日志
Completed 406 Not Acceptable in 91ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/items_controller.rb:33:in `create'
帮帮我,伙计们。我用谷歌搜索了整个互联网
最佳答案
对于那些仍在使用谷歌搜索的人...它帮助我指定了默认值:=> {format: 'js'} 用于 routes.rb 中的 ajax 操作。
post 'myaction' => 'mycontroller#myaction', defaults: { format: 'js' }
关于javascript - "ActionController::UnknownFormat"作为将 AJAX 与 format.js 结合使用的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28257924/