如何在URL中传递多个参数?

时间:2019-07-24 15:15:32

标签: ruby-on-rails

我正在尝试为项目创建自定义路径,我发送ID以及月份和年份来显示一些数据,这些参数是通过form_for发送的,并提供了一个糟糕的网址: http://localhost:3000/havings_discount_record/employee?utf8=%E2%9C%93&having%5Bemployee_id%5D=1&having%5Bhavings_month%5D=7&having%5Bhavings_year%5D=2019&commit=Busqueda

我尝试将路线从以下位置更改

get 'havings_discount_record/employee', to: 'havings_discount_record#record_per_employee', as: 'record_per_employee'

至: get 'havings_discount_record/employee/:employee_id/:havings_month/:havings_year', to: 'havings_discount_record#record_per_employee', as: 'record_per_employee' 但后来我记得那不是那样,按提交按钮后发送数据

 <%= form_for @havings, url: record_per_employee_path, method: :get  do |data| %>
    <%= data.text_field :employee_id %>
    <%= data.text_field :havings_month %>
    <%= data.text_field :havings_year %>
    <%= data.submit "Busqueda"%>
  <% end %>

我想让url尽可能简单,只显示如下内容:

http://localhost:3000/havings_discount_record/employee/1/7/2019

1 个答案:

答案 0 :(得分:1)

最后我尝试了其他方法,我以这种方式在控制器中创建了一个方法(这是简化的示例):

def search
  @employee_id = params[:legal_vacation][:employee_id]
  redirect_to employee_pool_path(@employee_id)
end

我进行路由并将表单数据发送到此方法,这样您就可以在应用程序中使用单个:id并避免使用讨厌的查询字符串。 希望这对您将来或将来有所帮助。如果您找到其他方法,请告诉我。

相关问题