添加新列后是否还有另一种重构方法:Sinatra Web App

Is there another way to refactor after adding new column: Sinatra Web App

我对使用 Sinatra Framework 还是个新手。我被要求添加一个新列 :description,并重构我的 POST 请求中的当前代码行以编辑新列表以包含 :description 列:@list.update(params.select{|k|k== "name"})

我不确定不仅要重构它以包含 :description,还要让它更短。

这是完整的 POST 请求块

post "/lists/:id" do
  redirect_if_not_logged_in
  @list = List.find(params[:id])
  unless List.valid_params?(params)
    redirect "/lists/#{@list.id}/edit?error=invalid list"
  end
  @list.update(params.select{|k|k== "name"})
  redirect "/lists/#{@list.id}"
end

我确实毫无问题地添加了新列,并且知道要在我的 EditNew 表单中放入什么,控制器操作是我卡住的地方。

更新: 我找到了我自己问题的答案。 我重构了我原来的 post 请求以反映此更改:

post "/lists/:id" do
   @list = List.find(params[:id])
if @list.update(params)
  redirect "/lists/#{@list.id}"
else
  redirect "/lists/edit"

结束 结束