ログインを押したら、ログインのフォームからインデックスのフォームに飛びたい
ログインを押したらこのようなエラーが発生します。
params is missing or value is empty:post
Ruby
1 # Only allow a list of trusted parameters through. 2 def post_params 3 params.require(:post).permit(:title, :body, :image, :email, :password) 4 end 5end
DBをrails c→Post.allで確認したら、emailもpasswordもカラムがあることがわかったので、valueを入れる必要があるのですが、どこに入れればいいのでしょうか。
Ruby
1Rails.application.routes.draw do 2 resources :posts 3 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 4 5 get "login" => "posts#login_form" 6 post "login" => "posts#login" 7 post "logout" => "posts#logout" 8 root 'posts#index' 9end 10
Ruby
1class PostsController < ApplicationController 2 before_action :set_post, only: [:show, :edit, :update, :destroy] 3 4 # GET /posts 5 # GET /posts.json 6 def index 7 @posts = Post.all 8 end 9 10 # GET /posts/1 11 # GET /posts/1.json 12 def show 13 end 14 15 # GET /posts/new 16 def new 17 @post = Post.new 18 end 19 20 # GET /posts/1/edit 21 def edit 22 end 23 24 # POST /posts 25 # POST /posts.json 26 def create 27 @post = Post.new(post_params) 28 29 respond_to do |format| 30 if @post.save 31 format.html { redirect_to @post, notice: '投稿しました。' } 32 format.json { render :show, status: :created, location: @post } 33 else 34 format.html { render :new } 35 format.json { render json: @post.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /posts/1 41 # PATCH/PUT /posts/1.json 42 def update 43 respond_to do |format| 44 if @post.update(post_params) 45 format.html { redirect_to @post, notice: '投稿を更新しました。' } 46 format.json { render :show, status: :ok, location: @post } 47 else 48 format.html { render :edit } 49 format.json { render json: @post.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /posts/1 55 # DELETE /posts/1.json 56 def destroy 57 @post.destroy 58 respond_to do |format| 59 format.html { redirect_to posts_url, notice: '投稿を削除しました。' } 60 format.json { head :no_content } 61 end 62 end 63 64 def login_form 65 end 66 67 def login 68 @post = Post.find_by(email: params[:email], password: params[:password]) 69 if @post 70 flash[:notice] = "ログインしました" 71 session[:post_id] = @post.id 72 redirect_to("/login/index") 73 else 74 @error_message ="メールアドレスまたはパスワードが間違っています" 75 @email = params[:email] 76 @password = params[:password] 77 render("users/login/form") 78 end 79 end 80 81 def logout 82 session[:post_id] = nil 83 flash[:notice] = "ログアウトしました" 84 redirect_to("/login") 85 end 86 87 private 88 # Use callbacks to share common setup or constraints between actions. 89 def set_post 90 @post = Post.find(params[:id]) 91 end 92 93 # Only allow a list of trusted parameters through. 94 def post_params 95 params.require(:post).permit(:title, :body, :image, :email, :password) 96 end 97end 98

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。