我有一个comments_controller
,它使用inherited_resources
来处理此模型:Comment (belongs_to Shop and belongs_to User)
和Shop (belongs_to User)
。 Rails 4.1.1和Inherited_resources v是1.5.0。Ruby on Rails Inherited_resources multiple belongs_to
航线有:
resources :shop do
resources :comments, only: [:create, :destroy]
end
但是,下面的代码无法正常工作:
class CommentsController < InheritedResources::Base
before_filter :authenticate_user!
nested_belongs_to :user, :shop
actions :create, :destroy
def create
@comment = build_resource
@comment.shop = Shop.find(params[:hotel_id])
@comment.user = current_user
create!
end
def destroy
@hotel = Shop.find(params[:hotel_id])
@comment = Comment.find(params[:id])
@comment.user = current_user
destroy!
end
private
def permitted_params
params.permit(:comment => [:content])
end
Rspec的意见是测试创建/删除告诉我Couldn't find User without an ID
。
感谢您的任何帮助。
UPD一个失败的测试:
let(:user) { FactoryGirl.create(:user) }
let(:shop) { FactoryGirl.create(:shop, user: user) }
describe "comment creation" do
before { visit shop_path(shop) }
describe "with invalid information" do
it "should not create a comment" do
expect { click_button "Post a comment" }.not_to change(Comment, :count)
end
end
这将有助于何况你是哪个的Rails的版本。这个gem [继承资源](https://github.com/josevalim/inherited_resources)非常古老,它的作者建议不要将它用于Rails 3和更高版本。 – San 2014-09-19 14:23:21
@San I更新。 Rails是4.1.1 – 2014-09-19 14:39:21