【问题标题】:How to pass the params:id in the url explicitly?如何在 url 中显式传递 params:id?
【发布时间】:2011-04-26 12:26:11
【问题描述】:

我有一个用户模型和帖子模型。我想显示属于每个用户的帖子,
通过 URL 说 http://localhost:3000/my_posts/1" 但 Rails 报告错误说

http://localhost:3000/my_posts/1 没有匹配的路由存在。

Rails 会查看完整的 url:“http://localhost:3000/my_posts/1”作为路由,但我的
打算就像 "http://localhost:3000/my_posts/" 应该被视为 url 路由,而 '1' 应该被视为具有 params:id 在这种情况下它代表 User_id。
我有路线匹配
“/my_posts” => “用户#view_posts”。

用户的动作例程view_posts在通过
触发时工作正常 “http://localhost:3000/users/1”
这里将 1 作为参数 id,将 /user 作为路由。
而且我还可以通过
在“/user”路由中显示该特定用户的帖子 渲染局部

我正在使用 Rails 版本 3。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:
    match "/my_posts/:id", :to => "user#view_posts"
    

    【讨论】:

      【解决方案2】:
      map.connect 'my_posts', :controller => ..., :action => ...
      

      【讨论】:

      • 嗨 Nikita 我使用的是 Rails 版本 3,并且我设置了以下路线:-匹配“/my_posts”=>“user#view_posts”。但我的问题是如何将参数 id 传递给该控制器的操作。
      • 你没有。您只需调用 '/my_posts?param1=123&param2=abc' 并且 param1param2 将被传递到 params 哈希中。您没有在路由声明中指定 param1param2
      猜你喜欢
      • 2019-05-26
      • 2020-06-13
      • 2023-04-07
      • 2011-08-27
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多