![](/img/trans.png)
[英]Rails CORS: ActionController::RoutingError (No route matches [OPTIONS] "/batches"):
[英]ActionController::RoutingError (No route matches
我正在尝试添加或删除嵌套的has_many对象,如下所示
class Question < ActiveRecord::Base
has_many :comments
end
= form_for @question, :url => {:action => "create"} do |f|
= f.label :name
= f.text_field :name
#comments
= link_to 'Add Comment", add_comment_question_path, method: :get
= f.submit
:javascript
$('#add_comment').click(function() {
$('#comments').append("#{escape_javascript(render(:partial => "comment"))}");
});
在我的_comment.html.haml中
= fields_for @question.comments do |c|
= c.label :msg
= c.text_field :msg
在我的控制器中
def add_comment
@question.comments << Comment.new
end
在routes.rb中
resources :questions do
get :add_comment, :on => :member
end
但是我在加载question/new.html.haml
时遇到路由错误。 我还跑rake routes
获取正确的指定网址。 为什么我会收到此错误?
我假设错误在add_comment_question_path
。 此命名路线需要一个问题资源才能传递给它,例如add_comment_question_path(@question)
。 但这在您的情况下仍然不起作用,因为您尝试以相同的形式创建@question
。
你有没有尝试过:
= form_for @question, :url => questions_path do |f|
questions_path
应该是从routes.rb
自动生成的。 这些可以通过运行rake routes
来枚举,它显示可用的路由,然后可以使用route_path
或route_url
来唤起这些route_url
。
高温超导
尝试添加方法来发布:
form_for(@question, :url => {:action => "create"}, :html => {:method => "post"} ) do |f|
并将routes.rb编辑为:
在routes.rb中
resources :questions do
member do
get :add_comment
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.