我的应用程序中有一个form_for正在提交错误的网址。
<% @posts.each do |post| %>
<form class="col-md-12">
<% comment = post.comments.build %>
<%= form_for [post, comment], :url => post_comments_path(post), html: {class: "col-md-12", method: "POST"} do |comment_fields| %>
<div class="form-group">
<%= comment_fields.text_area :content, placeholder: "Write a comment...", class: "form-control", rows: "4" %>
</div>
<div class="form-group">
<a class="col-md-1" href="#">Cancel</a>
<%= comment_fields.submit "Comment", class: "col-md-3 pull-right btn btn-primary" %>
</div>
<% end %>
<% end %>
这是我的PostsController索引方法:
def index
@user = User.find(params[:user_id])
@posts = @user.posts
end
我希望它能转到POST /posts/:post_id/comments(.:format) comments#create
但它正在发送此请求:
Started GET "/users/6/posts?utf8=%E2%9C%93&authenticity_token=WgbO5FhAA0ZhXFdk%2FizEbqTVxg2HW9VDY7aUKQTLmy3qLXLC8EH9%2FKr0w53TcId2KYoJmU7uodCFJOIDufno8g%3D%3D&comment%5Bcontent%5D=qqqqqqqqqqqq111&commit=Comment"
以下是我的路线:
Prefix Verb URI Pattern Controller#Action
logout GET /logout(.:format) sessions#destroy
login GET /login(.:format) sessions#new
session POST /session(.:format) sessions#create
new_session GET /session/new(.:format) sessions#new
edit_session GET /session/edit(.:format) sessions#edit
GET /session(.:format) sessions#show
PATCH /session(.:format) sessions#update
PUT /session(.:format) sessions#update
DELETE /session(.:format) sessions#destroy
post_likes GET /posts/:post_id/likes(.:format) likes#index
POST /posts/:post_id/likes(.:format) likes#create
new_post_like GET /posts/:post_id/likes/new(.:format) likes#new
edit_like GET /likes/:id/edit(.:format) likes#edit
like GET /likes/:id(.:format) likes#show
PATCH /likes/:id(.:format) likes#update
PUT /likes/:id(.:format) likes#update
DELETE /likes/:id(.:format) likes#destroy
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
user_posts GET /users/:user_id/posts(.:format) posts#index
POST /users/:user_id/posts(.:format) posts#create
有没有办法用form_for做到这一点?我可以使用form_tag post_likes_path,使用方法:delete选项,但我想自动将@ comment.post_id设置为帖子。
答案 0 :(得分:0)
请勿将form
保留在另一个form
内。
试试这个:
<% @posts.each do |post| %>
<div class="col-md-12">
<% comment = post.comments.build %>
<%= form_for [post, comment], :url => post_comments_path(post), html: {class: "col-md-12"} do |comment_fields| %>
<div class="form-group">
<%= comment_fields.text_area :content, placeholder: "Write a comment...", class: "form-control", rows: "4" %>
</div>
<div class="form-group">
<a class="col-md-1" href="#">Cancel</a>
<%= comment_fields.submit "Comment", class: "col-md-3 pull-right btn btn-primary" %>
</div>
<% end %>
</div>
<% end %>