简体   繁体   中英

“ActionController::UnknownFormat” as a result of using AJAX with format.js

I have models: idea,item,product. I'm trying to add Products to Ideas through Items in a view of Idea's editing. My edit.html.erb - Idea

<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>

My items 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

idea.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

My "create.js.erb"

$('#items').html("<%= escape_javascript render(@idea.items) %>");

When I put "format.html {redirect_to :back}" in def create (items_controller) everything goes OK, but without AJAX=(

Logs

Completed 406 Not Acceptable in 91ms

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/items_controller.rb:33:in `create'

Help me, guys. I have googled the whole internet

对于那些仍在使用Google搜索的人...它帮助我为route.rb中的ajax操作指定默认值:=> {format:'js'}。

post 'myaction' => 'mycontroller#myaction', defaults: { format: 'js' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM