私は、公開され編集可能なRailsのwikiアプリケーションに取り組んでいます。私は記事コントローラとドラフトコントローラを持っています。誰かが記事の「編集」をクリックすると、元の記事の内容を含む新しい草案を作成し、ユーザーが「保存」をクリックするとデータベーステーブルに保存したいと思います。どのように私はこれを行うに行くかもしれないかについての任意のアイデア?私は数日間それに固執してきた。Railsの記事/ポストドラフト
現在、各記事はカテゴリ、サブカテゴリ、およびhas_manyドラフトに属しています。
データベース・スキーマ:
ActiveRecord::Schema.define(version: 20160723153357) do
create_table "articles", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "content"
t.integer "category_id"
t.integer "subcategory_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "drafts", force: :cascade do |t|
t.string "title"
t.text "content"
t.integer "category_id"
t.integer "subcategory_id"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "subcategories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
end
Articles_controller:
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# GET /articles
# GET /articles.json
def index
if params[:category].blank? && params[:subcategory].blank?
@articles = Article.all.order("created_at DESC")
elsif params[:subcategory].blank?
@category_id = Category.find_by(name: params[:category]).id
@articles = Article.where(category_id: @category_id).order("created_at DESC")
else
@subcategory_id = Subcategory.find_by(name: params[:subcategory]).id
@articles = Article.where(subcategory_id: @subcategory_id).order("created_at DESC")
end
end
# GET /articles/1
# GET /articles/1.json
def show
end
# GET /articles/new
def new
@article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
@parameters = article_params
@parameters[:category] = Category.find_by(id: Subcategory.find_by(id: article_params[:subcategory_id]).category_id)
@article = Article.new(@parameters)
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1
# PATCH/PUT /articles/1.json
def update
respond_to do |format|
if @article.update(article_params)
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { render :show, status: :ok, location: @article }
else
format.html { render :edit }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
@article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:title,:content,:subcategory_id)
end
end
Drafts_controller:
class DraftsController < ApplicationController
before_action :set_draft, only: [:show, :edit, :update, :destroy]
# GET /drafts
# GET /drafts.json
def index
@drafts = Draft.all
end
# GET /drafts/1
# GET /drafts/1.json
def show
end
# GET /drafts/new
def new
@draft = Draft.new
end
# GET /drafts/1/edit
def edit
end
# POST /drafts
# POST /drafts.json
def create
@parameters = draft_params
@parameters[:article_id] = params[:article_id]
@parameters[:subcategory_id] = 2
@parameters[:category_id] = 2
@draft = Draft.new(@parameters)
respond_to do |format|
if @draft.save
format.html { redirect_to @draft, notice: 'Draft was successfully created.' }
format.json { render :show, status: :created, location: @draft }
else
format.html { render :new }
format.json { render json: @draft.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /drafts/1
# PATCH/PUT /drafts/1.json
def update
respond_to do |format|
if @draft.update(draft_params)
format.html { redirect_to @draft, notice: 'Draft was successfully updated.' }
format.json { render :show, status: :ok, location: @draft }
else
format.html { render :edit }
format.json { render json: @draft.errors, status: :unprocessable_entity }
end
end
end
# DELETE /drafts/1
# DELETE /drafts/1.json
def destroy
@draft.destroy
respond_to do |format|
format.html { redirect_to drafts_url, notice: 'Draft was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_draft
@draft = Draft.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def draft_params
params.require(:draft).permit(:title, :content)
end
end
Articleモデル:
class Article < ApplicationRecord
belongs_to :category
belongs_to :subcategory
has_many :drafts
end
ドラフトモデル:
class Draft < ApplicationRecord
belongs_to :category
belongs_to :subcategory
belongs_to :article
end
コントローラ/ビューコードを投稿してください。 – Ven
また、article.rbとdraft.rbをスローすることもあります。 – MarsAtomic
申し訳ありませんが、完了です。 – Alex