最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - "ActionController::UnknownFormat" as a result of using AJAX with format.js - Stack Overflow

matteradmin6PV0评论

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

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

Share Improve this question asked Jan 31, 2015 at 23:53 Oleg VidyaevOleg Vidyaev 231 silver badge4 bronze badges 4
  • it's likely you need to write the else, or remove if , see stackoverflow./a/22944769/1197775 – sites Commented Feb 1, 2015 at 0:12
  • Try to change respond_to ... format.js with render "create.js.erb". Also handle situation when item is not saved – Pavel Evstigneev Commented Feb 1, 2015 at 3:46
  • Thank you for advices, but ... I tried to put "else" statement in my def create, but i didn't hepl me, cause statement of saving item was performing. Removing "if" also didn't give me anything. – Oleg Vidyaev Commented Feb 1, 2015 at 11:38
  • Pavel, render of "create.js.erb" didn't help me. When i pushed to the button, rails brought me to the page with this code: $('#items').html("\n<li>MDC-4220C<form action=\"/items/219\" class=\"button_to\" data-remote=\"true\" method=type=\"submit\" value=\"X\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"k9LNh3DAV/bBxoMjkzHa6G0Qmlv5uBfLe9l80MAfwJg=\" /><\/div><\/form><\/li>\n<li>KIC-301<form action=\"/items/220\" class=\"button_to\" data-remote=\"true\" method=\"post\"><div><input name=\"_method\" type=\"hidden\" value=\"delete\" /><input type=\"submit\" AND SO ON( – Oleg Vidyaev Commented Feb 1, 2015 at 11:40
Add a ment  | 

1 Answer 1

Reset to default 6

For those who are still googling... It helped me to specify defaults: => {format: 'js'} for ajax actions in routes.rb.

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

comment list (0)

  1. No comments so far