Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3:如何对路由路径进行编码_Ruby On Rails_Ruby_Routes - Fatal编程技术网

Ruby on rails Rails 3:如何对路由路径进行编码

Ruby on rails Rails 3:如何对路由路径进行编码,ruby-on-rails,ruby,routes,Ruby On Rails,Ruby,Routes,我正在创建一个rails应用程序,但我无法找出或找到这个问题的答案 路线是什么: @example=example.find_by_notIdparams[:notId] 我希望我的路线看起来比/example/1更好,我更希望它是/notId,其中notId是title或其他一些非ID int。通常我会使用show_path,但在我的html中不起作用。这是我的代码,当我将其硬编码到URL localhost:3000/notId时,它会工作,但我需要一种通过链接路由它的方法。有什么想法吗 展

我正在创建一个rails应用程序,但我无法找出或找到这个问题的答案

路线是什么:

@example=example.find_by_notIdparams[:notId]

我希望我的路线看起来比/example/1更好,我更希望它是/notId,其中notId是title或其他一些非ID int。通常我会使用show_path,但在我的html中不起作用。这是我的代码,当我将其硬编码到URL localhost:3000/notId时,它会工作,但我需要一种通过链接路由它的方法。有什么想法吗

展示

路线

match '/:notId', to: 'example#show', via: 'get'
_example.html.erb

<div class="example">
 <% Movie.all.each do |example| %>
  <%pic = example.title + ".jpg"%>
  <%= link_to image_tag(pic), show_path(example) %>
 <% end %>
e</div>

您可能需要覆盖路由中的:id参数:

resources :examples, param: :title
这将生成如下路由:

/examples/:title
/examples/:title/edit
那么在你看来,

example_path(example.title)
或者,您可以将方法更改为用于在模型中构建URL的参数:

class Example < ActiveRecord::Base
  def to_param
    title
  end
end

这使您可以继续使用example\u pathexample而不使用.title

谢谢您的帮助,我只需将html更改为example\u pathexample.title
class Example < ActiveRecord::Base
  def to_param
    title
  end
end