如何使用多模型,导轨中的一个控制器

时间:2018-12-04 05:19:02

标签: ruby-on-rails ruby

我想在一个控制器中使用三个模型 如果单击标签'삭제',我想在管理控制器中执行free_destroy操作。

当我单击链接时,会出现以下错误页面:

  

ActiveRecord :: RecordNotFound在ManagementsController#free_destroy

     

并且找不到ID = 1的管理

我该如何解决? 为什么会发生此错误?

请为我回答。

控制器

class ManagementsController < ApplicationController
load_and_authorize_resource param_method::params_management
def index
 @users = User.all
 @freemanages = Freemanagement.all
end

def freepost_delete
 @freemanage = Freemanagement.new(params.require(:modal).permit(:content, :title_id))
 @freemanage.save()

 redirect_to :back
end

def secret_delete
 # @secretmanage = Secretmanagement.new(params.require(:modal).permit(:content, :title_id))
 # @secretmanage.save()

 # redirect_to :back
end

def show
 freemanage = Freemanagement.find(params[:id])

 redirect_to freemanagements_path(freemanage), method: :delete
end

def free_destroy
 freemanage = Freemanagement.find(params[:id])
 freemanage.destroy()

 redirect_to freepost_path(freemanage.title_id), method: :delete
end

def refuse_freepost
 freepost = Freepost.find(params[:remodal][:title_id])
 freemanages = Freemanagement.all
 freemanages.each do |freemanage|
   if (freemanage.title_id.to_i != freepost.id)
     next
   else
     freemanage.destroy()
   end
 end
 redirect_to :back
end

def  refuse_secretpost
end
end

index.html

<head>

</head>
<body>
 <header>
   <!-- 공지사항 작성 글 -->
   <%= link_to '익명겟', secretposts_path %>
   <%= link_to '자유겟', freeposts_path %>
   <%= current_user.last_name + current_user.first_name %>
 </header>
 <section>
   <% @users.each do |user| %>
     <%= user.uid %>
     <%= user.first_name %>
     <%= user.last_name %>
     <%= user.roles_name%>
   <% end %>
 </section>
 <section>
   <% @freemanages.each do |freemanage| %>
     <%= link_to '게시글보기', freepost_path(freemanage.title_id) %>
     <%= freemanage.content %>
     <a href='/freemanagements/<%= freemanage.id %>/delete'>삭제</a>
     <br />
   <% end %>
 </section>
 <section>
   <!-- 익명게시판 관련 게시글 섹션 -->
 </section>
 <script>
 </script>
</body>

routes.rb

Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

root 'homes#index'

resources :freeposts do
  resources :free_comments
end

resources :secretposts do
  resources :secret_comments
end

resources :free_comments
resources :secret_comments

get '/managements' => 'managements#index'
get '/freemanagements/:id' => 'managements#show'
post '/freemanagements' => 'managements#freepost_delete'
get '/freemanagements/:id/delete' => 'managements#free_destroy'
post '/freemanagements/refuse' => 'managements#refuse_freepost'
post '/secretmanagements' => 'managements#secretpost_delete'

get 'login', to: redirect('/auth/google_oauth2'), as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
# get 'home', to: 'home#show'
# get 'me', to: 'me#show', as: 'me'
end

0 个答案:

没有答案