以下のページを参考に小説サイトを作成しているのですがうまくいません。
Ruby on RailsでユーザIDと入力データの関連付けをする
具体的に、「undefined method `Short' for #User:0x007fde39eb2e30」と言った感じのエラーがでています。
###エラー内容
NoMethodError in ShortsController#index undefined method `shorts' for #<User:0x007f004de722d8> Extracted source (around line #8): # GET /shorts.json def index @shorts = current_user.shorts.all end # GET /shorts/1
###問題のcontrollerのコードです。
class ShortsController < ApplicationController before_action :set_short, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! # GET /shorts # GET /shorts.json def index @shorts = current_user.shorts.all end # GET /shorts/1 # GET /shorts/1.json def show end # GET /shorts/new def new @short = Short.new end # GET /shorts/1/edit def edit end # POST /shorts # POST /shorts.json def create @short = Short.new(short_params) respond_to do |format| if @short.save format.html { redirect_to @short, notice: 'Short was successfully created.' } format.json { render :show, status: :created, location: @short } else format.html { render :new } format.json { render json: @short.errors, status: :unprocessable_entity } end end end # PATCH/PUT /shorts/1 # PATCH/PUT /shorts/1.json def update respond_to do |format| if @short.update(short_params) format.html { redirect_to @short, notice: 'Short was successfully updated.' } format.json { render :show, status: :ok, location: @short } else format.html { render :edit } format.json { render json: @short.errors, status: :unprocessable_entity } end end end # DELETE /shorts/1 # DELETE /shorts/1.json def destroy @short.destroy respond_to do |format| format.html { redirect_to shorts_url, notice: 'Short was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_short @short = current_user.shorts.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def short_params params.require(:short).permit(:title, :description, :noval, :user_id) end end
###追加補足
Userモデルはdevise+Omniauthで実装済みです。
###開発環境
- Ruby on Rails5.1.2
###主に使用しているGem
- devise
- omniauth
エラーが出た箇所がエラーメッセージに記載されていると思います。これではどこでエラーになっているのか判断できません。
回答3件
あなたの回答
tips
プレビュー