找不到'id'= testuser的帖子

时间:2016-11-03 07:32:28

标签: ruby-on-rails ruby

当我删除帖子时,我收到错误“找不到帖子'id'= testuser”。我认为这是一个路由错误,因为我认为正在运行的id应该是Post的而不是User?我无法解决这个问题。我可以创建帖子,但出于同样的原因无法删除或编辑。

如果需要,我可以提供更多信息。

PostController#destroy中提取的源代码是:

def set_post
@work = Work.find(params[:id])

的routes.rb

Rails.application.routes.draw do

devise_for :users, :controllers => { :registrations => "registrations" }
resources :posts do
end

# Define route URL
root 'pages#index'

# Define routes for Pages
get '/home' => 'pages#home'

get '/user/:id' => 'pages#profile'

get '/new' => 'posts#new'

Pages_controller.rb

class PagesController < ApplicationController

def index
end

def home
end

def profile
if (User.find_by_username(params[:id]))
  @username = params[:id]
else
  redirect_to root_path, :notice=> "User not found!"
end

@posts = Post.all.where("user_id = ?", User.find_by_username(params[:id]).id)
@newPost = Post.new

end

end

Posts_controller.rb

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]


def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id #assign post to the user who created it
    respond_to do |f|
        if (@post.save)
            f.html { redirect_to "", notice: "Post created!" }
        else
            f.html { redirect_to "", notice: "Error: Post Not Saved" }
        end
    end
end

def show

end

def edit
respond_to do |format|
  if @post.update(post_params)
    format.html { redirect_to @post, notice: 'Your post was successfully updated.' }
    format.json { render :show, status: :ok, location: @post }
  else
    format.html { render :edit }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end
end

def destroy
    @post.destroy
    respond_to do |format|
  format.html { redirect_to root_path, notice: 'Post was successfully deleted.' }
  format.json { head :no_content }
end
end

private

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

def post_params #allows certain data to be passed via form
    params.require(:post).permit(:user_id, :title, :description, :image)
end

end

Profile.html.erb

...
<%= link_to('Delete', post_path, :method => :delete) %>
...

终端

                  Prefix Verb   URI Pattern                    Controller#Action
    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
       user_password POST   /users/password(.:format)      devise/passwords#create
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                     PATCH  /users/password(.:format)      devise/passwords#update
                     PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        registrations#cancel
   user_registration POST   /users(.:format)               registrations#create
new_user_registration GET    /users/sign_up(.:format)       registrations#new
edit_user_registration GET    /users/edit(.:format)          registrations#edit
                     PATCH  /users(.:format)               registrations#update
                     PUT    /users(.:format)               registrations#update
                     DELETE /users(.:format)               registrations#destroy
               posts GET    /posts(.:format)               posts#index
                     POST   /posts(.:format)               posts#create
            new_post GET    /posts/new(.:format)           posts#new
           edit_post GET    /posts/:id/edit(.:format)      posts#edit
                post GET    /posts/:id(.:format)           posts#show
                     PATCH  /posts/:id(.:format)           posts#update
                     PUT    /posts/:id(.:format)           posts#update
                     DELETE /posts/:id(.:format)           posts#destroy
                root GET    /                              pages#index
                home GET    /home(.:format)                pages#home
                     GET    /user/:id(.:format)            pages#profile
             connect GET    /connect(.:format)             pages#connect
                help GET    /help(.:format)                pages#help
            messages GET    /messages(.:format)            pages#messages
                 new GET    /new(.:format)                 posts#new

1 个答案:

答案 0 :(得分:1)

您没有在删除链接

中传递ID或实例
<%= link_to('Delete', post_path, :method => :delete) %>

应该在删除链接上传递post实例,以便在找到它时找到帖子

<%= link_to('Delete', post_path(post), :method => :delete) %>

<%= link_to('Delete', post_path(post.id), :method => :delete) %>