没有路由匹配{:action =>" index",:controller =>" comments",:post_id => nil}缺少必需的密钥:[:post_id]

时间:2015-09-29 19:11:25

标签: ruby-on-rails ruby

这是我遇到的问题,Haven能够绕过它。现在登录帐户时会发生这种情况。我昨晚才解决这个问题。 enter image description here

这是我的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

继承我的评论_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 %>

这是我的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

如果你们确实需要看到更多的代码然后让我知道,我会发布它!非常感谢你!

1 个答案:

答案 0 :(得分:0)

问题是您是否尝试通过传递/posts/:post_id/comments来创建如下所示的网址:form_for([@post, @comment])。没有@comment未保存到数据库中,但您使用@post必须已保存到数据库,因为您可以&#39 ; t创建没有@post ID的网址。

保存@post后,它会有一个ID,因此您可以生成路线:例如,/posts/32/comments

检查您的dashboard.html.erb文件,了解您使用@posts和呈现comments/_form.html.erb的位置。您可能有一个post对象,您应该在表单中使用它:form_for([post, @comment])

您可能还想从@post = Post.new控制器操作中删除#dashboard行。