我的应用程序具有如下强大的参数。
如何使这段代码易于阅读?
这一行有很多属性
我的 Controller
def create
@schedule = current_user.schedules.build(schedule_params)
...
end
...
def schedule_params
params.require(:schedule).permit(:title, :departure_date, rooms_attributes: [:id, :_destroy, :room, :room_address, :day, :ccy, :amount, events_attributes: [:id, :_destroy, :start_at, :end_at, :title, :detail, :category, :ccy, :amount, amounts_attributes: [:id, :_destroy, :ccy, :amount]]])
end
如果您能给我任何建议,我们将不胜感激。
最佳答案
除了字符串,ruby 主要忽略空格。这使得可以轻松地将一行代码分成多个部分。您可以利用它来使您的参数更易于阅读:
def schedule_params
params.require(:schedule).permit(
:title, :departure_date,
rooms_attributes: [
:id, :_destroy, :room, :room_address, :day, :ccy, :amount,
events_attributes: [
:id, :_destroy, :start_at, :end_at,
:title, :detail, :category, :ccy, :amount,
amounts_attributes: [:id, :_destroy, :ccy, :amount]
]
])
end
关于ruby-on-rails - rails : How to make strong parameters easy to read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836633/