LoginSignup
2
0

More than 3 years have passed since last update.

railsで投稿できないのを対処しました

Last updated at Posted at 2021-02-27

スクリーンショット 2021-02-26 21.36.25.png

構築環境

macOS Big sur
ruby 2.6.5
rails 6.0.0

何があったのか

最初は、スクリーンショット 2021-02-26 21.36.25.png
こんな感じで、paramsが入っていないっていうエラー。しかしcontrollerを見てみてもおかしいところはなさそう
しかし、おかしいところを見つけましたね。
controllの記述を間違えてしまいました。

↓こちらは修正前の、投稿に関するcontroller

posts_controller.rb
class PostsController < ApplicationController
  before_action :authenticate_user!
  before_action :find_post, only: [:edit, :update, :show, :destroy]

    def index
        @posts = Post.all
      end

      def new
      @post = Post.new
    end

    def edit
      @post = Post.find(post_params)
    end

    def create
      return redirect_to new_profile_path,alert: "プロフィールを登録してください" if current_user.profile.blank?
      @post = current_user
      @post = Post.create params.require(:post).permit(:content, images: []) 
      if @post.save
        redirect_to root_path,notice:'投稿に成功しました'
        else
          render :new
        end
      end

      def update
        if @post.update(post_params)
          redirect_to root_path
        else
          render :edit
        end
      end

      def destroy
        if @post.destroy
          redirect_to root_path,alert: '投稿を削除しました'
        else
          redirect_to root_path
        end
      end

      private
        def post_params
          params.require(:post).permit(:content, images: [])
        end

        def find_post
          @post = Post.find(params[:id])
        end

        def force_redirect_unless_my_post
          return redirect_to root_path,alert:'権限がありません'if @post.user != current_user
        end
end

修正後↓

class PostsController < ApplicationController
  before_action :authenticate_user!
  before_action :find_post, only: [:edit, :update, :show, :destroy]

    def index
        @posts = Post.all
      end

      def new
      @post = Post.new
    end

    def edit
      @post = Post.find(post_params)
    end

    def create
      return redirect_to new_profile_path,alert: "プロフィールを登録してください" if current_user.profile.blank?
      @post = current_user
      @post = Post.create params.require(:post).permit(:content, images: []) 
      if @post.save
        redirect_to root_path,notice:'投稿に成功しました'
        else
          render :new
        end
      end

      def update
        if @post.update(post_params)
          redirect_to root_path
        else
          render :edit
        end
      end

      def destroy
        if @post.destroy
          redirect_to root_path,alert: '投稿を削除しました'
        else
          redirect_to root_path
        end
      end

      private
        def post_params
          params.require(:post).permit(:content, images: [])
        end

        def find_post
          @post = Post.find(params[:id])
        end

        def force_redirect_unless_my_post
          return redirect_to root_path,alert:'権限がありません'if @post.user != current_user
        end
end

20行目の、
@post = Post.create.params.require(:post).permit(:content, images: [])
の部分を、
@post = Post.create params.require(:post).permit(:content, images: [])に変えました。
上の間違った文章だと、Postのcreateメソドッドのparamsを撮ってこいってなってしまうので、そりゃあparamsなんてないよって上のエラーの画像みたいに怒られるでしょうね。
しかし、したのようにしたことで、Post.createと、paramsのストラロングパラメーターの部分と分離するという本来伝えたかった意味になるので、通ったわけです。

もうひとつやったことがありました。

class Post < ApplicationRecord
    has_many_attached :images
    belongs_to :user, optional: true
end

modelの部分のアソシエーションは、userと結びつけていますなので、userと投稿であるpostは、1対多になります
↓が、userに関するmodelです。deviceを使っているので、それに関する記述がありますがここでは説明しないので気にしないでください

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  has_many :posts
  has_one :profile, dependent: :destroy
end

has_many :postsとしているので、userは、一人でもたくさんの投稿ができるようになっています。

下が

post.rb
class Post < ApplicationRecord
    has_many_attached :images
    belongs_to :user, optional: true
end

optional: trueをuserにつけることで、親クラス(user)の外部キーのnilを許可するようにすることで、投稿機能の中にあるsaveメソッドをきのうさせることができました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0