我正在设置一些路由,以向评论控制器中的create动作发出AJAX请求。我在表单中包含了对象“用户”和“电影”中包含了正确的Rails URL。当我单击创建时,它给我错误
ActionController::RoutingError (No route matches [POST] "/users/7/movies/12")
。
<div class="panel panel-default">
<div class="panel-heading">Write Review</div>
<div class="panel-body">
<%= simple_form_for user_movie_create_review_path(@movie.user, @movie), remote: true do |f| %>
<%= f.input :text, as: :text, input_html: {rows: 5, class: 'simple_form_review'}%>
<%= f.button :submit, "Submit Review", class: 'btn btn-primary' %>
<% end %>
</div>
</div>
</div>
resources :users do
resources :movies do
post 'create_review', to: 'reviews#create'
end
end
class Review < ApplicationRecord
belongs_to :user
belongs_to :movie
validates :content, presence: true
end
耙道:
user_movie_create_review POST /users/:user_id/movies/:movie_id/create_review(.:format) reviews#create
请求:
Started POST "/users/7/movies/12" for ::1 at 2019-09-20 19:15:53 +0200
答案 0 :(得分:1)
您需要将评论对象传递给simple_form_for
:
<%= simple_form_for @review, url: user_movie_create_review_path(@movie.user, @movie), remote: true do |f| %>
..
<% end %>