0

通常、単純な解決策を伴う単純なエラーですが、行き詰まっているようです:

投稿#コントローラー:

class PostsController < ApplicationController

def index
 @posts = Post.all
end

def show
 @post = Post.find params[:id]
end

def new
 @post = Post.new
end

def create
 @post = Post.create post_params

 if @post.save
  redirect_to posts_path, :notice => "Your post was saved!"
 else
  render 'new'
 end

end

private
 def post_params
  params.require(:post).permit(:title, :content)
 end

def edit
 @post = Post.find params[:id]
end

def update
 @post = Post.find params[:id]

 if @post.update_attributes params[:post]
  redirect_to posts_path
 else
  render 'edit'
 end
end

def destroy
 @post = Post.find params[:id]
 @post.destroy

 redirect_to posts_path, :notice => "Your post has been deleted"
end

end

ルート.rb:

Blog::Application.routes.draw do

 resources :posts

end

レーキルート:

Prefix Verb   URI Pattern               Controller#Action
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

投稿ビュー、index.html.slim:

h1 Blog
- @posts.each do |post|
 h2 = link_to post.title, post
 p = post.content
 p = link_to 'Edit', edit_post_path(post)
 p = link_to 'Delete', post, :confirm => "Are you sure?", method: :delete
 br

p = link_to 'Add a new post', new_post_path

それでも、ブラウザ内で次のようなエラーが表示され続けます。

不明なアクションです。PostsController のアクション「destroy」が見つかりませんでした

Rails 4 に更新してから、これらの基本的な問題がいくつか発生しているようです。

4

2 に答える 2

2

PostsController#destroyprivate宣言の下にあるため、プライベート メソッドです。呼び出し方法には制限があります。

def destroy ... end単語の上に移動してみてくださいprivate(適切な場合は、別の方法でそのルートを保護してください)。何らかの理由でまだプライベート メソッドを呼び出す必要がある場合は、次#sendのように使用できます。

PostsController.new.send :destroy # and any arguments, comma-separated

(#sendこの方法を使用しても Rails コントローラーには意味がありませんが、別の機会に役立つかもしれません!)

于 2013-10-15T04:53:11.447 に答える
0

posts_controller.rb で、このコードを使用してみてください

def destroy
  Post.find(params[:id]).destroy
  redirect_to posts_path
end

そして index.html.erb で使用

<%= link_to "Delete", post, :data => {:confirm => "Are you sure?"}, :method => :delete %>

Rails 4.2.5.1を使用してそれを理解しました。これは Rails 4.x 固有のものだと思いますが、他のバージョンでも動作する可能性があります。

于 2017-04-02T09:17:37.807 に答える