【发布时间】:2013-02-12 12:22:51
【问题描述】:
我正在尝试添加或删除嵌套的 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 获取正确的指定网址。为什么会出现这个错误?
【问题讨论】:
-
你可以编辑你的问题的html代码吗?
-
指定您遇到的路由错误。没有路线匹配什么?
标签: jquery ruby-on-rails ruby-on-rails-3 rails-routing