admin管理员组文章数量:1026989
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
withrender "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
1 Answer
Reset to default 6For 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' }
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
withrender "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
1 Answer
Reset to default 6For 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' }
版权声明:本文标题:javascript - "ActionController::UnknownFormat" as a result of using AJAX with format.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661123a2161897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论