質問編集履歴
1
スキーマ情報、コントローラー情報の追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -73,7 +73,94 @@ | |
| 73 73 | 
             
              belongs_to :tag
         | 
| 74 74 | 
             
            end
         | 
| 75 75 | 
             
            ```
         | 
| 76 | 
            +
            ```
         | 
| 77 | 
            +
            スキーマ
         | 
| 78 | 
            +
            create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
         | 
| 79 | 
            +
              t.text "article_title", null: false
         | 
| 80 | 
            +
              t.text "article_content", null: false
         | 
| 81 | 
            +
              t.integer "user_id", null: false
         | 
| 82 | 
            +
              t.integer "category_id"
         | 
| 83 | 
            +
              t.integer "sub_category_id"
         | 
| 84 | 
            +
              t.string "article_thumbnail"
         | 
| 85 | 
            +
              t.integer "impressions_count", default: 0
         | 
| 86 | 
            +
              t.datetime "created_at", null: false
         | 
| 87 | 
            +
              t.datetime "updated_at", null: false
         | 
| 88 | 
            +
            end
         | 
| 89 | 
            +
            ```
         | 
| 90 | 
            +
            ```
         | 
| 91 | 
            +
            コントローラー
         | 
| 92 | 
            +
            class Users::ArticlesController < ApplicationController
         | 
| 93 | 
            +
              before_action :require_login
         | 
| 94 | 
            +
              before_action :logedin_admin?
         | 
| 76 95 |  | 
| 96 | 
            +
              def show
         | 
| 97 | 
            +
                @article = Article.find(params[:id])
         | 
| 98 | 
            +
              end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
              def new
         | 
| 101 | 
            +
                @article = Article.new
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              def edit
         | 
| 105 | 
            +
                @article = Article.find(params[:id])
         | 
| 106 | 
            +
              end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              def create
         | 
| 109 | 
            +
                @article = Article.new(article_params)
         | 
| 110 | 
            +
                respond_to do |format|
         | 
| 111 | 
            +
                  begin
         | 
| 112 | 
            +
                    ActiveRecord::Base.transaction do
         | 
| 113 | 
            +
                      @article.save!
         | 
| 114 | 
            +
                    end
         | 
| 115 | 
            +
                    # 正常処理
         | 
| 116 | 
            +
                    format.html { redirect_to articles_path }
         | 
| 117 | 
            +
                    format.json { render :index, location: @article }
         | 
| 118 | 
            +
                  rescue ActiveRecord::RecordInvalid
         | 
| 119 | 
            +
                    # バリデーション処理
         | 
| 120 | 
            +
                    @article.valid?
         | 
| 121 | 
            +
                    format.html { render :new }
         | 
| 122 | 
            +
                    format.json { render json: @article.errors }
         | 
| 123 | 
            +
                  rescue => e
         | 
| 124 | 
            +
                    Rails.logger.error e
         | 
| 125 | 
            +
                    Rails.logger.error e.backtrace.join("\n")
         | 
| 126 | 
            +
                    Raven.capture_exception(e)
         | 
| 127 | 
            +
                    flash.now[:danger] = "保存に失敗しました"
         | 
| 128 | 
            +
                    format.html { render :new }
         | 
| 129 | 
            +
                  end
         | 
| 130 | 
            +
                end
         | 
| 131 | 
            +
              end
         | 
| 132 | 
            +
             | 
| 133 | 
            +
              def update
         | 
| 134 | 
            +
                @article = Article.find(params[:id])
         | 
| 135 | 
            +
                if @article.update!(article_params)
         | 
| 136 | 
            +
                  redirect_to root_url
         | 
| 137 | 
            +
                else
         | 
| 138 | 
            +
                  render :edit
         | 
| 139 | 
            +
                end
         | 
| 140 | 
            +
              end
         | 
| 141 | 
            +
             | 
| 142 | 
            +
              def destroy
         | 
| 143 | 
            +
                @article = Article.find(params[:id])
         | 
| 144 | 
            +
                @article.destroy!
         | 
| 145 | 
            +
                redirect_to users_articles_path
         | 
| 146 | 
            +
              end
         | 
| 147 | 
            +
             | 
| 148 | 
            +
              private
         | 
| 149 | 
            +
             | 
| 150 | 
            +
              def article_params
         | 
| 151 | 
            +
                params.require(:article).permit(:article_title, :article_content, :article_thumbnail, :article_thumbnail_cache, :category_id, :sub_category_id, {:tag_ids => []}, :user_id)
         | 
| 152 | 
            +
              end
         | 
| 153 | 
            +
             | 
| 154 | 
            +
              def logedin_admin?
         | 
| 155 | 
            +
                redirect_to articles_path unless current_user.admin?
         | 
| 156 | 
            +
              end
         | 
| 157 | 
            +
             | 
| 158 | 
            +
              def set_user
         | 
| 159 | 
            +
             | 
| 160 | 
            +
              end
         | 
| 161 | 
            +
            end
         | 
| 162 | 
            +
            ```
         | 
| 163 | 
            +
             | 
| 77 164 | 
             
            ### 試したこと
         | 
| 78 165 | 
             
            [ActiveRecordでidがないテーブルにデータを保存しようとすると"nil is not a symbol"とか言われる](http://shinonome.hatenablog.jp/entry/2015/07/22/231534)
         | 
| 79 166 | 
             
            ### 補足情報(FW/ツールのバージョンなど)
         | 
