ruby on rails - Create a Post_category for a Post a choose my post with collection post_category -
i created post_category table add category specifics posts.
for example created post_categories countries, japan or china. , want create post come culture or mode countries japan or china. focused on post_categories countries , below code did.
i created postcategory
, here migration , model
create_table "post_categories", force: :cascade |t| t.string "name" t.string "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false end class postcategory < activerecord::base has_many :posts, dependent: :destroy names = ["japon", "chine", "corée du sud", "moyen orient", "indien"] validates :name, inclusion: { in: postcategory::names, allow_nil: false } end
and created post
postcategory
foreign key, here migration , model
create_table "posts", force: :cascade |t| t.string "cover" t.string "subtitle" t.string "title" t.text "introduction" t.text "body" t.text "conclusion" t.string "tag" t.string "source" t.string "link" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "post_category_id" end class post < activerecord::base belongs_to :user belongs_to :post_category, dependent: :destroy tags = ["design", "mode", "tendance", "life-style", "tradition", "gastronomie", "insolite", "technologie"] validates :tag, inclusion: { in: post::tags, allow_nil: false } mount_uploader :cover, imageuploader end
i want create category simple form collection , want displayed on post show#view
here post_categories controller
class postcategoriescontroller < applicationcontroller # before_action :set_post_category, only: [:show, :new, :create, :destroy] def show @post_category = postcategory.find(params[:id]) end def index @post_categories = postcategory.all end def create @post_category = post_categories.new(post_category_params) if @post_category.save redirect_to @post else render 'post_categories/show' end end def new @post_category = postcategory.new end def edit end def update end def destroy @post_category = postcategory.find(params[:id]) @post_category.destroy redirect_to post_path(@post) end private # def set_post # @post = post.find(params[:post_id]) # end def find_post_category @post_category = postcategory.find(params[:id]) end def post_category_params params.require(:post_category).permit(:name, :description) end end
and here posts controller
class postscontroller < applicationcontroller before_filter :authenticate_user!, except: [:index, :show] before_action :find_post, only: [:show, :edit, :update, :destroy] def index @posts = post.all end def show # @alert_message = "vous lisez #{@post.title}" end def new # if current_user , current_user.admin? @post = post.new # else # redirect_to posts_path # end end def create # if current_user , current_user.admin? @post = @post_category.posts.new(post_params) #@post = current_user.posts.new(post_params) if @post.save redirect_to @post else render :new end # else # render 'shared/404.html.erb' # end end def edit end def update if @post.update(post_params) redirect_to @post else render :edit end end def destroy @post.destroy redirect_to :back end private # def find_post # @post = post.find(params[:id]) # end def set_post_category @post_category = postcategory.find(params[:post_category_id]) end def post_params params.require(:post).permit(:title, :subtitle, :introduction, :body, :cover, :tag, :post_category_id) end end
i don't know views create , how calling post new#view
because configured routes that, , need post_category_id
.
resources :post_categories resources :posts end
that's have use following path
post_category_posts /post_categories/:post_category_id/posts(.:format) posts#index post /post_categories/:post_category_id/posts(.:format) posts#create new_post_category_post /post_categories/:post_category_id/posts/new(.:format) posts#new edit_post_category_post /post_categories/:post_category_id/posts/:id/edit(.:format) posts#edit post_category_post /post_categories/:post_category_id/posts/:id(.:format) posts#show patch /post_categories/:post_category_id/posts/:id(.:format) posts#update put /post_categories/:post_category_id/posts/:id(.:format) posts#update delete /post_categories/:post_category_id/posts/:id(.:format) posts#destroy post_categories /post_categories(.:format) post_categories#index post /post_categories(.:format) post_categories#create new_post_category /post_categories/new(.:format) post_categories#new edit_post_category /post_categories/:id/edit(.:format) post_categories#edit post_category /post_categories/:id(.:format) post_categories#show patch /post_categories/:id(.:format) post_categories#update put /post_categories/:id(.:format) post_categories#update delete /post_categories/:id(.:format) post_categories#destroy
i want add category on show#view post , create multisearch access find posts added specific category. thank help
the way have setup relations since has big flaw - post can belong single category since id stored on posts table.
instead commonly use many many relationship:
class post < activerecord::base has_many :categories, :categories has_many :post_categories end class category < activerecord::base has_many :posts has_many :posts, through: :categories end class categorization < activerecord::base belongs_to :post belongs_to :category validates_uniqueness_of :category_id, scope: :post_id end
here categorizations
table acts join table links post
, category
- post can have number of categories , vice versa.
added:
to create join model , migration create table do:
rails g model categorization post:belongs_to category:belongs_to
using belongs in generator default creates foreign keys post_id
, category_id
.
you might want add compound uniqueness constraint in migration well.
def change # ... add_index(:categorizations, [:post_id, :category_id], unique: true) end
your on right track categoriescontroller
not use nested route create posts. instead might want use plain old rails controller , let user select categories via check boxes:
<%= form_for(@post) |f| %> <%= f.collection_check_boxes :category_ids, category.all, :id, : name %> <% end %>
you add category_ids
param whitelist:
def post_params params.require(:post) .permit(:title, :subtitle, :introduction, :body, :cover, :tag, category_ids: [] ) end
the weird category_ids: []
syntax because want allow array of scalar values.
querying posts category id can done so:
post.joins(:categories).where(category: 1)
you can select multiple categories passing array:
post.joins(:categories).where(category: [1, 2, 3])
to hook view use link (for single category) or form (yes forms can used get) checkboxes or selects.
dealing textual input bit trickier. naive implementation be:
post.joins(:categories).where(category: { name: params[:search_query] })
however user input have match exactly. there several gems provide search features active record. wait feature until have bit more experience can tricky implement.
see:
Comments
Post a Comment