从另一个控制器删除 post?
Delete post from another controller?
我的应用程序中有两个控制器。截至目前 - 用户和 Post.The 用户控制器目前只有这些方法 - 索引、新建、创建和显示。我仅在 users#show 视图页面中显示用户 posts。
我想添加删除 post 的功能。我在 post 的控制器中添加了删除 post 的方法....但是当我点击删除按钮时它显示错误 The action 'destroy' could not be找到 UsersController
这是真的,我在用户控制器中没有 destroy 方法。
如何仅使用 post 控制器删除 post?我不想在这里使用用户控制器,删除 post 因为 - 我想添加功能以在将来删除用户
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(set_params)
if @user.save
UserNotifierMailer.send_signup_email(@user).deliver
flash[:success] ="Success"
redirect_to new_sessions_path and return
else
render 'new'
end
end
def show
@user = User.find(params[:id])
@posts = @user.posts
end
private
def set_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def post_params
params(:post.require).permit(:title, :body)
end
end
用户#show
<div class="col-md-8">
<h1> Posts ( <%= @posts.count %> ) </h1>
<%= @posts.each do |p| %>
<div class="form-field">
<h4><b><%= p.title %></b> <%= link_to "Edit", edit_post_path(p.id), class: "btn
btn-warning col-md-2-offset", style: "align-right" %><%= link_to "Delete",
@post , method: :delete,data: { confirm: "You sure? "}, class: "btn btn-danger
col-md-2-offset", style: "align-right" %></h4>
<%= p.body %>
</div>
<% end %>
Post 控制器 - 删除方法
def destroy
@post = Post.find(params[:id])
@post.destroy
flash.now[:success] = "Succesfully Deleted"
redirect_to user_path(current_user)
end
路线
Rails.application.routes.draw do
root 'posts#index'
resources :users
resources :posts
resource :sessions, only: [:new, :create, :destroy]
resources :passwords, only: [:new, :create, :edit, :update]
end
Rake 路由:DELETE /posts/:id(.:format) posts#destroy
我认为你的意思是 p
而不是 @post
:
<%= link_to "Delete", p, ... %>
在您看来 @post
很可能是 nil
,这与 link_to
混淆,因此出现错误。
我的应用程序中有两个控制器。截至目前 - 用户和 Post.The 用户控制器目前只有这些方法 - 索引、新建、创建和显示。我仅在 users#show 视图页面中显示用户 posts。
我想添加删除 post 的功能。我在 post 的控制器中添加了删除 post 的方法....但是当我点击删除按钮时它显示错误 The action 'destroy' could not be找到 UsersController
这是真的,我在用户控制器中没有 destroy 方法。
如何仅使用 post 控制器删除 post?我不想在这里使用用户控制器,删除 post 因为 - 我想添加功能以在将来删除用户
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(set_params)
if @user.save
UserNotifierMailer.send_signup_email(@user).deliver
flash[:success] ="Success"
redirect_to new_sessions_path and return
else
render 'new'
end
end
def show
@user = User.find(params[:id])
@posts = @user.posts
end
private
def set_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def post_params
params(:post.require).permit(:title, :body)
end
end
用户#show
<div class="col-md-8">
<h1> Posts ( <%= @posts.count %> ) </h1>
<%= @posts.each do |p| %>
<div class="form-field">
<h4><b><%= p.title %></b> <%= link_to "Edit", edit_post_path(p.id), class: "btn
btn-warning col-md-2-offset", style: "align-right" %><%= link_to "Delete",
@post , method: :delete,data: { confirm: "You sure? "}, class: "btn btn-danger
col-md-2-offset", style: "align-right" %></h4>
<%= p.body %>
</div>
<% end %>
Post 控制器 - 删除方法
def destroy
@post = Post.find(params[:id])
@post.destroy
flash.now[:success] = "Succesfully Deleted"
redirect_to user_path(current_user)
end
路线
Rails.application.routes.draw do
root 'posts#index'
resources :users
resources :posts
resource :sessions, only: [:new, :create, :destroy]
resources :passwords, only: [:new, :create, :edit, :update]
end
Rake 路由:DELETE /posts/:id(.:format) posts#destroy
我认为你的意思是 p
而不是 @post
:
<%= link_to "Delete", p, ... %>
在您看来 @post
很可能是 nil
,这与 link_to
混淆,因此出现错误。