"submit" %> 还有我的 Controller :-6ren">
gpt4 book ai didi

ruby-on-rails - 重构 params[...] 的许多 if 语句,在 Controller 操作中

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:12 25 4
gpt4 key购买 nike

我有这样的代码,用于在我的表单中进行链式选择查看索引操作:

<%= form_tag do %>
<%= collection_select(*@brands_select_params) %>
<%= collection_select(*@car_models_select_params) %>
<%= collection_select(*@production_years_select_params) %>
<% # Пока еще никто ничего не выбрал %>
<%= submit_tag "Send", :id => "submit", :name => "submit" %>

还有我的 Controller :

class SearchController < ApplicationController
def index
@brands = Brand.all
@car_models = CarModel.all

if (params[:brand].blank?)
@brands_select_params = [:brand, :id, @brands, :id, :name, :prompt => "Выбирай брэнд"]

if params[:car_model].blank?
@car_models_select_params = [:car_model, :id, @car_models, :id, :name, { :prompt => "Model" }, \
{ :disabled => "disabled" }]
@production_years_select_params = [:production_year, :id, @car_models, :id, :name, { :prompt => "Year" }, \
{ :disabled => "disabled" }]
end
else
@brands_select_params = [:brand, :id, @brands, :id, :name, { :selected => params[:brand][:id] } ]
if params[:car_model].blank?
@car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
{ :prompt => "And model now" } ]
@production_years_select_params = [:production_year, :id, @car_models, :id, :name, { :prompt => "Year" }, \
{ :disabled => "disabled" } ]
else
@car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
{ :selected => params[:car_model][:id] } ] unless params[:car_model][:id].empty?
@production_years_select_params = [:production_year, :id, CarModel.find(params[:car_model][:id]).production_years, :id, :year, \
{ :prompt => "And year now" } ] unless params[:car_model][:id].empty?
end
end
end
end

如您所见,我的 Controller 代码中有太多 if。我要在那里添加更多条件。之后,任何阅读该代码的人都会脑部腐败。所以我只想以真正的 Ruby 方式实现它,但不知道如何实现。请帮忙,伙计们。我应该如何重构这些废话?

最佳答案

我认为问题的很大一部分是您在 Controller 中做的太多了。生成标记(以及包括为表单助手构建参数列表的 IMO)应该在 View 和 View 助手中完成。所以:

module SearchHelper
def brand_select brands, options={}
collection_select :brand, :id, brands, :id, :name, :options
end

def car_model_select car_models, options={}
collection_select :car_model, :id, car_models, :id, :name, options
end

def production_year_select years, options={}
collection_select :production_year, :id, years, :id, :year, options
end
end

然后你可以把你的 Controller 缩减成这样:

def index
@brands = Brand.all
@car_models = CarModel.all

@selected_brand_id = params[:brand] && params[:brand][:id]
@selected_car_model_id = params[:car_model] && params[:car_model][:id]

@production_years = @selected_car_model_id ?
[] : CarModel.find(@selected_car_model_id).production_years
end

在你看来:

<%= brand_select @brands, :prompt   => "Выбирай брэнд",
:selected => @selected_brand_id
%>
<%= car_model_select @car_models, :prompt => "Model",
:selected => @selected_car_model_id
%>
<%= production_year_select @production_years, :prompt => "Year",
:selected => @selected_car_id
%>

我怀疑您可以使用 form_for and fields_for 进一步简化它并完全摆脱助手,但这在一定程度上取决于您的模型关联的设置方式。

关于ruby-on-rails - 重构 params[...] 的许多 if 语句,在 Controller 操作中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7548215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com