0

I am working on a rails project that takes user input. I want to be able to show the user values in the url and i am having trouble how to do this.

For example, I retrieve an input "limit"

limit = params[:limit] (where :limit = 10)

And I want the url to be something like this

www.someurl.com/myproject/limit="10" or something

4

2 回答 2

1

您要查找的 URL 是 www.someurl.com/myproject?limit=10

假设您有一个 myproject 路径,您可以:

link_to "Link Text", myproject_path(limit: 10)

在您的控制器中,您将:

limit = params[:limit]

如果您想为 limit 设置默认值,您可以使用:

limit = params[:limit] || 10

或者

limit = params.fetch(:limit, 10)
于 2013-06-20T14:10:30.643 回答
0

对于带有参数的 get 请求,这将是一个完美的用例。例如,如果您有一个称为myproject_path返回路径“/myproject”的 url 帮助程序,那么您可以执行以下操作:

myproject_path(:limit => 10) # this returns "/myproject?limit=10"

您现在可以像在问题中一样访问此值(通过params[:limit]

希望这能回答你的问题?

于 2013-06-20T14:14:34.527 回答