這是我遇到的問題,解決它。現在登錄賬戶時發生這種情況。直到昨晚,我還沒有遇到這個問題。 沒有路線匹配{:action =>「index」,:controller =>「comments」,:post_id => nil}缺少必需的鍵:[:post_id]
這裏是我的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
如果你們需要再看一段代碼,那就讓我知道,我會發布它!提前感謝你!
我做到了。那就是那種形式。 –
如果你去這裏http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html並搜索「如果你的資源有定義的關聯,例如」,你會看到你正在使用的語法的一個例子。並且它表示只有(在你的情況下)'@post'是一個現有對象(保存在數據庫中)時,它纔會起作用,你將爲該帖子添加評論。我在代碼中看到的是@post也是一個新對象。 – fanta
根據我們從屏幕截圖中可以看到的短堆棧跟蹤,您是否可以添加comments_controller.rb文件,因爲這似乎是問題所在。另外,您的development.log中的更多行可能會有助於顯示比截圖顯示更多的錯誤。 – Phil