0

このモデルを使用および処理するがcomments_controllerあります: 、および。 Rails 4.1.1および Inherited_resources v は 1.5.0 です。inherited_resourcesComment (belongs_to Shop and belongs_to User)Shop (belongs_to User)

ルートは次のとおりです。

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失敗したテストの 1 つ:

  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
4

1 に答える 1