【发布时间】:2014-12-08 17:12:11
【问题描述】:
我收到此错误:没有路由匹配 [POST] "/posts/5/cmets/new"。
如何重定向到我的帖子/cmets/new 路由 ( /posts/:post_id/cmets/new(.:format) ) 的 GET 操作而不是 POST??
这是我对 cmets 控制器的创建操作:
def create
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
@comment.user_id = current_user.id #or whatever is you session name
if @comment.save
redirect_to @post
else
redirect_to new_post_comment_path
end
end
end
如果您查看错误消息,您可以看到我的创建操作正在寻找我的 new_post_comment_path 路由的 POST 版本,而它应该是 GET。
rake 路由的一些输出:
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment_path GET /posts/:post_id/comments/new(.:format) comments#new
我的路线是怎么做的:
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
resources :comments
end
评论_form.html.erb:
<%= form_for @comment, url: {action: "new"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_area :body, size: "40x12" %>
<%= f.submit "Add Comment" %>
<% end %>
当我将操作从新更改为创建时,我收到一条新的缺少模板错误消息:
缺少模板 cmets/create, application/create with {:locale=>[:en], :formats=> [:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}。在以下位置搜索:*“app/views”*“.rvm/gems/ruby-2.1.2/gems/devise-3.3.0/app/views”
评论new.html.erb
<h1>New Comment</h1>
<%= render 'comments/form' %>
感谢您的帮助。
【问题讨论】:
-
你的帖子和cmets模特有关系吗?请在此处添加您的 routes.rb 文件
-
您是否在另一个
create方法中定义了create方法?
标签: ruby-on-rails ruby ruby-on-rails-4