my route.rb
post 'home/create'
get 'home/create'
my HomeController
def create
@review_n = Review.create(review_params)
if @review_n.errors.empty?
respond_to do |format|
format.js { render 'create', locals: {review_name: @review_n.review_n, review_body: @review_n.review_body} }
end
else
render 'index'
end
end
my create.js.erb
$(function() {
$(".wrap-body").append("<div> tmp </div>");
});
rails say: ActionController::UnknownFormat in HomeController#create
I want send data in my html.erb without reload page. Help me, please!
UPD:
my html.rb
<%= form_tag home_create_path, :method => 'post', :remote => true do %>
<%= text_area_tag 'review[review_body]', nil %>
<%= text_field_tag 'review[review_name]', nil %>
<%= submit_tag 'send' %>
<% end %>
Is your code hitting the render 'index'
call that is outside of the respond_to
block?
Normally you would put all render
calls inside the respond_to
block, so it's obvious that all paths of your logic can respond to all expected formats:
def create
@review_n = Review.create(review_params)
respond_to do |format|
if @review_n.errors.empty?
format.js { render 'create', locals: {review_name: @review_n.review_n, review_body: @review_n.review_body} }
else
format.js { render 'index' }
end
end
end
This requires having both a create.js.erb
and an index.js.erb
(for the error case).
Also, as @sahil recommended, your routes.rb should not declare get 'home/create'
- this action modifies data, so it isn't safe to make it accessible via GET
.
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.