简体   繁体   中英

No route matches {:action=>“index”, :controller=>“comments”, :post_id=>nil} missing required keys: [:post_id]

This is the issue I am having, Haven't been able to get around it. Now this happens while logging into the account. I haven't had this issue before til last night. 在此处输入图片说明

Here is my users_controller.rb,

class UsersController < ApplicationController
  before_action :set_user, only: [:edit, :update, :destroy]
  before_action :correct_user,   only: [:edit ]

  after_action :signed_in_after_register, only: :create 

  def index
    @users = User.all
    @user = User.find(session[:user_id])
    if params[:search]
        @users = User.search(params[:search]).order("created_at DESC")
      else
        @users = User.all.order('created_at DESC')
      end
  end

  def dashboard 
    @user = User.find(session[:user_id]) unless session[:user_id] == ""
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3)
    @comment = Comment.new 
    @post = Post.new 
  end 

  def newsfeed
    @user = User.find(session[:user_id]) unless session[:user_id] == nil
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3)
  end

  def nav
    @user = User.find(session[:user_id])
  end
  def posts
    @user = User.find(session[:user_id])
    @posts = @user.posts
  end

  def destroy
    @user = User.find(session[:user_id]) unless session[:user_id] == ""
    redirect_to login_path, notice: "You're not logged in" unless @user 
  end

  def welcome
    @user = User.find(params[:user_id]) unless session[:user_id] == ""
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @user = User.find(session[:user_id])
  end
  def show 
    @user = User.find(params[:user_id]) unless session[:user_id] == ""
    redirect_to login_path, notice: "You're not logged in" unless @user 
    @posts = @user.posts.order("created_at DESC").limit(3)
    @comment = Comment.new 
    @post = Post.new  
  end

  def new
    @user = User.new
    @post = Post.new(params[:post_id])
  end

  def edit
    @user = User.find(params[:user_id]) if params[:user_id]
    redirect_to @dashboard_path unless @user
  end

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to dashboard_path, notice: 'User was successfully created!' }
        format.json { render :profile, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    if @user == current_user
      respond_to do |format|
        if @user.update(user_params)
          format.html { redirect_to dashboard_path, notice: 'User was successfully updated.' }
          format.json { render :profile, status: :ok, location: @user }
        else
          format.html { render :edit }
          format.json { render json: @user.errors, status: :unprocessable_entity }
        end
      end
    else
      redirect_to dashboard_path, notice: 'You do not have permission to edit the profile of another user.'
    end
  end

  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_user
      @user = User.find(params[:id])
    end
    def correct_user
      @user = User.find(params[:id]) unless session[:user_id] == ""

    end
    def signed_in_after_register 
      session[:user_id] = @user.id 
    end

    def user_params
      params.require(:user).permit(:first_name, :last_name, :bio, :comments, :password, :password_confirmation, :email, :age, :profile_picture, :post, :body)
    end
end

Heres my comments _form.html.erb,

<%= form_for([@post, @comment]) do |f| %>
    <p>
        <%= f.text_area :body, placeholder: "Write a comment!" %>
    </p>
    <br>
    <p> <%= f.submit %> </p>

<% end %>

And here is my routes.rb,

Rails.application.routes.draw do

  root 'welcome#welcome'

  get 'login' => 'sessions#login', :as => :login 

  get 'dashboard' => 'users#dashboard', :as => :dashboard

  post 'logging/user' => 'sessions#create'  

  get 'logout' => 'sessions#destroy', :as => :logout 

  get 'about' => 'about'

  get 'newsfeed' => 'users#newsfeed'

  resources :users, except: :show
  get 'profile/:user_id' => 'users#show', as: :profile

  get 'location' => 'location#location' 

  resources  :posts do
    resources :comments
  end

  get 'index' => 'posts#index'

  get 'register' => 'users#new', :as => :register

end

If you guys do need to see anymore code then just let me know, I will post it! Thank you so much in advance!

The problem is you're trying to create a URL that looks like this: /posts/:post_id/comments by passing form_for([@post, @comment]) . It's OK that @comment isn't saved to the database, but the @post you use must already be saved to the database because you can't create that URL without @post having an ID.

Once @post is saved, it'll have an ID, so you can generate the route: for example, /posts/32/comments .

Check your dashboard.html.erb file for where you're using @posts and rendering comments/_form.html.erb . You may have a post object available, and you should use it in your form instead: form_for([post, @comment]) .

You'll probably also want to remove the @post = Post.new line from your #dashboard controller action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM