Since can't write unknown attribute category_id
appeared when posting with the post function, I posted it for recording.
https://qiita.com/cawaz3/items/e755a58177212f2aca6c Refer to this article for table structure etc. Implemented an intermediate table and linked the tag id at the time of posting.
It is set to transition to the post confirmation screen after describing the posted content on the posting screen, but it seems that the above error occurs and the transition does not work well.
Difference in column name In post.controller.rb, it was described as category_id, but when I checked the table, it was categories_id.
ruby:post.controller.rb
def confirm
@post = Post.new(post_params)
session[:category_ids] = @post.category_ids
return if @post.valid?
flash.now[:alert] = 'There was a mistake in the input.'
render :new
end
def back
@post = Post.new(post_params)
render :new
end
private
def post_params
params.require(:post).permit(:post_photo,
:post_photo_cache,
:place_name,
:area,
:street_address,
:time,
:regular_holiday,
:url,
:station,
:shop_name,
category_ids: []).merge(user_id: current_user.id )
end
end
python
select * from post_category_relations;
id | posts_id | categories_id | created_at | updated_at
----+----------+---------------+------------+------------
(0 rows)
Change the description of the model rails db: migrate: reset
python
class CreatePostCategoryRelations < ActiveRecord::Migration[5.2]
def change
create_table :post_category_relations do |t|
t.references :post, foreign_key: true, null: false
t.references :categories, foreign_key: true, null:false ⇦ before change
t.references :category, foreign_key: true, null:false ⇦ after change
t.timestamps
end
end
end