我正在建立一个诗歌网站。 有两种不同类型的诗歌:著名的或业余的。
我构建了CRUD函数以显示所有诗歌(著名的和业余的,没有区别的),并且按预期工作(请参见下面的PoetrisController
代码)。
现在,我想让用户选择是否只看业余诗歌或著名诗歌。 基本上,用户单击导航栏中的链接“ Amateur”或“ Famous”,然后将其重定向到仅列出业余或著名诗歌的新页面。
我的问题是:我应该创建另一个控制器(例如PoetriesFamousController
)并在其中创建索引函数以仅显示著名的诗歌,还是可以使用已有的PoetriesController
如果用户单击导航栏中的链接,仅显示“著名诗歌”?
PoetriesController:
class PoetriesController < ApplicationController
skip_after_action :verify_authorized, only: [:home, :about, :newsletter, :disclaimer, :new, :create]
skip_before_action :authenticate_user!, only: [:home, :about, :newsletter, :disclaimer, :new, :create]
before_action :set_poetry, only: [:show, :edit, :update, :destroy,]
before_action :authenticate_user!, except: [:index, :amateur_poetries]
def index
if params[:search]
@poetries = policy_scope(Poetry).search(params[:search]).order("created_at DESC").limit(30)
else
@poetries = policy_scope(Poetry).order("RANDOM()").limit(30)
end
end
def show
authorize @poetry
end
def new
@poetry = Poetry.new
end
def create
Poetry.create(poetry_params)
redirect_to poetries_path
end
def edit
authorize @poetry
end
def update
@poetry.save
redirect_to poetry_path(@poetry)
end
def destroy
@poetry.destroy
redirect_to poetries_path
end
private
def poetry_params
params.require(:poetry).permit(:title, :author, :body, :poster, :country)
end
def set_poetry
@poetry = Poetry.find(params[:id])
end
end
Poetries.rb
class Poetry < ApplicationRecord
def self.search(search)
where("lower(title) LIKE ? OR lower(author) LIKE ? OR lower(country) LIKE ? OR lower(born) LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
end
end
Routes.rb
get 'poetries', to: 'poetries#index', as: :poetries
get "poetries/new", to: "poetries#new"
post "poetries", to: "poetries#create"
get "poetries/:id/edit", to: "poetries#edit"
patch "poetries/:id", to: "poetries#update"
get 'poetries/:id', to: 'poetries#show', as: :poetry
delete "poetries/:id", to: "poetries#destroy"
答案 0 :(得分:1)
最简单的事情是向控制器添加两个动作。
def famous
@poetries = #get the famous ones
render :index
end
def amateur
@poetries = #get the amateur ones
render :index
end
然后更新您的路线
get 'poetries', to: 'poetries#index', as: :poetries
get 'poetries/famous', to: 'poetries#famous'
get 'poetries/amateur', to: 'poetries#amateur
# rest of the routes
答案 1 :(得分:1)
这是您解决问题的一些主意
您认为(示例示例)
poetries type:
<%= select_tag :poetries_type, options_for_select(["Famous","Amateur"]), include_blank: true, :class => 'form-control' %>
在您的控制器中
def index
if params[:search]
if params[:poetries_type] == "Famous"
@poetries = Poetry.famous.search(params[:search]).order("created_at DESC").limit(30)
elsif params[:poetries_type] == "Amateur"
@poetries = Poetry.amateur.search(params[:search]).order("created_at DESC").limit(30)
else
@poetries = Poetry.search(params[:search]).order("created_at DESC").limit(30)
end
else
@poetries = policy_scope(Poetry).order("RANDOM()").limit(30)
end
end
Poetries.rb,为著名的业余爱好者添加两个范围
def self.amateur
where("poster != ?","Admin")
end
def self.famous
where("poster = ?","Admin")
end