【发布时间】:2017-12-26 18:56:32
【问题描述】:
在我的应用程序中,我正在管理员中创建类别(因此管理员创建、更新和销毁类别名称),然后当用户创建他们将选择的帖子时(或者我正在考虑使用复选框进行切换)帖子的类别。
我决定用一个有很多的帖子和类别来做这个实现。但我对工具有疑问:
- post_params;
- 添加类别然后销毁类别的方法
- 以及创建、更新和销毁帖子的部分。
我该如何实现呢?这是一种更好的方法吗?因此,如果有人帮助我,我将不胜感激。
Post.rb
class Post < ActiveRecord::Base
has_many :categorizations
has_many :categories, through: :categorizations
...
def add_category(category)
categorizations.create(category_id: category.id)
end
def remove_category(category)
categorizations.find_by(category_id: category.id).destroy
end
category.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, through: :categorizations
validates :name,
presence: true,
length: { minimum: 3, maximum: 30 },
uniqueness: true
end
分类.rb
class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
validates :post_id, presence: true
validates :category_id, presence: true
end
控制器/管理员/PostsController
class Admin::PostsController < Admin::ApplicationController
def new
@post = Post.new
@categories = Category.all.map{ |c| [c.name, c.id]}
end
def create
@post = Post.new(post_params)
@post.author = current_user
@post.categories << params[:category_id]
if @post.save
flash[:notice] = "Post has been created."
redirect_to @post
else
flash[:alert] = "Post has not been created."
render "new"
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:notice] = "Post has been deleted."
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title,
:subtitle,
:content,
:attachment,
:attachment_cache,
:remote_attachment_url,
:categorizations_attributes => [:id,
:post_id,
:category_id,
:_destroy,
:categories_attributes => [:id,
:name]
]
)
end
end
控制器/posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update]
def index
@posts = policy_scope(Post)
end
def show
authorize @post, :show?
end
def edit
authorize @post, :update?
end
def update
authorize @post, :update?
if @post.update(post_params)
flash[:notice] = "Post has been updated."
redirect_to @post
else
flash.now[:alert] = "Post has not been updated."
render "edit"
end
end
private
def set_post
@post = Post.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:alert] = "The post you were looking for could not be found."
redirect_to posts_path
end
def set_category
@category = Category.find(params[:category_id])
end
def post_params
params.require(:post).permit(:title, :subtitle, :content, :attachment, :attachment_cache, :remove_attachment, :remote_attachment_url, :category_id)
end
end
posts/_form.html.slim
= simple_form_for([:admin, @post], :html => { :multipart => true }) do |f|
= select_tag(:category_id, options_for_select(@categories), :prompt => "Select ad Category")
路线
Rails.application.routes.draw do
namespace :admin do
root 'application#index'
resources :posts, only: [:new, :create, :destroy]
resources :categories
resources :users do
member do
patch :archive
end
end
end
devise_for :users
root "posts#index"
resources :posts, only: [:index, :show, :edit, :update]
end
【问题讨论】:
标签: ruby-on-rails simple-form has-many-through simple-form-for