前提・実現したいこと
rails 5.1.6
has_many through で以下のようにモデルの関連付けを行っています。
Space <-> FeatureTagMap <-> Feature
space登録時に、collection_check_boxes を利用してfeaturesをidの配列でPOSTしているのですが、なぜかrollbackされてしまします。
Qiita等の記事で確認してみたところ、コードには問題がなさそうに思えるのですが、
参考にしている記事がrails 4に関するものが多かったので、現在は実装方法が異なっているのでしょうか。
また、具体的なエラーメッセージが出ないため、困ってしまってしまいました。
実装方法がわかる方がおられましたらご教示いただけますと幸いです。
また、該当のコードを記載しましたので、お気づきの点がありましたらアドバイスいただけますとありがたいです。
よろしくお願いいたします。
発生している問題・エラーメッセージ
Started POST "/cms/spaces" for 27.110.41.121 at 2019-02-25 16:47:46 +0900 Cannot render console from 27.110.41.121! Allowed networks: 39.110.218.66, 127.0.0.0/127.255.255.255, ::1 Processing by Cms::SpacesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Tyq0iSDPn1UIoQewU0rBFm+mdT5nAUQN8kPzhjObZwFdT1f1kowLTXqqwBZ9MsbIZOzXFYIOGsH/wKmRTTXzZg==", "space"=>{"client_id"=>"1", "city_id"=>"18", "name"=>"サンプルスペース-1", "feature_ids"=>["5", "6", "7"]}, "commit"=>"登録"} Client Load (0.1ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] Feature Load (0.2ms) SELECT "features".* FROM "features" WHERE "features"."id" IN (5, 6, 7) (0.0ms) begin transaction CACHE Client Load (0.0ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] City Load (0.1ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = ? LIMIT ? [["id", 18], ["LIMIT", 1]] Feature Exists (0.1ms) SELECT 1 AS one FROM "features" WHERE "features"."name" = ? AND ("features"."id" != ?) LIMIT ? [["name", "テスト"], ["id", 5], ["LIMIT", 1]] Feature Exists (0.1ms) SELECT 1 AS one FROM "features" WHERE "features"."name" = ? AND ("features"."id" != ?) LIMIT ? [["name", "テスト2"], ["id", 6], ["LIMIT", 1]] Feature Exists (0.1ms) SELECT 1 AS one FROM "features" WHERE "features"."name" = ? AND ("features"."id" != ?) LIMIT ? [["name", "テスト3"], ["id", 7], ["LIMIT", 1]] Space Exists (0.1ms) SELECT 1 AS one FROM "spaces" WHERE "spaces"."name" = ? LIMIT ? [["name", "サンプルスペース-1"], ["LIMIT", 1]] (0.1ms) rollback transaction
space.rb
class Space < ApplicationRecord belongs_to :client belongs_to :city has_many :feature_tag_maps has_many :features, through: :feature_tag_maps validates :name, presence: true, uniqueness: true validates :client_id, presence: true validates :city_id, presence: true end
feature_tag_map.rb
class FeatureTagMap < ApplicationRecord belongs_to :space belongs_to :feature validates :space_id, presence: true validates :feature_id, presence: true end
feature.rb
class Feature < ApplicationRecord has_many :feature_tag_maps has_many :spaces, through: :feature_tag_maps validates :name, presence: true, uniqueness: true include RankedModel ranks :row_order end
new.html.rb
<tr> <th>特徴</th> <td> <%= f.collection_check_boxes :feature_ids, Feature.all, :id, :name, include_hidden: false %> </td> </tr>
spaces_controller.rb
def create @space = Space.new(space_params) if @space.save flash[:success] = "#{@space.name} を登録しました。" redirect_to cms_space_url(@space) else render 'new' end end ・ ・ ・ private def space_params params.require(:space).permit(:name, :client_id, :city_id, feature_ids: []) end
試したこと
rails console で
space.features に Feature.allなどの配列を代入してsaveするとうまくいくようなのですが、
idのみの配列でsaveするとうまくいかないようです。
以下のような記事を参考にしました。
https://qiita.com/9m/items/b658ef002d14e404ed49
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。