前提・実現したいこと
投稿をセーブして表示したいです。
発生している問題・エラーメッセージ
NoMethodError in ChallengesController#create
undefined method `content' for #Challenge:0x007f6859acbb60 Did you mean? concern
def create @challenge = current_user.challenges.build(challenge_params) if @challenge.save ←エラー flash[:success] = 'メッセージを投稿しました。' redirect_to root_url else
該当のソースコード
class ChallengesController < ApplicationController before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def index if logged_in? @user = current_user @challenge = current_user.challenges.build # form_for 用 @challenges = current_user.challenges.order('created_at DESC').page(params[:page]) end end def create @challenge = current_user.challenges.build(challenge_params) if @challenge.save flash[:success] = 'メッセージを投稿しました。' redirect_to root_url else @challenges = current_user.challenges.order('created_at DESC').page(params[:page]) flash.now[:danger] = 'メッセージの投稿に失敗しました。' render 'toppages/index' end end def destroy @challenge.destroy flash[:success] = 'メッセージを削除しました。' redirect_back(fallback_location: root_path) end private def challenge_params params.require(:challenge).permit(:titile, :description) end def correct_user @challenge = current_user.challenges.find_by(id: params[:id]) unless @challenge redirect_to root_url end end end
<%= form_for(@challenge) do |f| %> <div class="form-group"> <%= f.text_area :titile, class: 'form-control', rows: 5 %> </div> <%= f.submit 'Post', class: 'btn btn-primary btn-block' %> <% end %>
class CreateChallenges < ActiveRecord::Migration[5.0] def change create_table :challenges do |t| t.string :titile t.text :description t.references :user, foreign_key: true t.timestamps end end end
class Challenge < ApplicationRecord belongs_to :user belongs_to :category validates :user_id, presence: true validates :content, presence: true, length: { maximum: 255 } validates_presence_of :titile validates_presence_of :body end
回答2件
あなたの回答
tips
プレビュー