在没有路径的嵌套路由中编辑

发布于 2024-12-14 18:58:04 字数 798 浏览 3 评论 0原文

使用:Rails 3.1.1 和 URL 的Friendly_id。

我认为这应该相当容易,但我无法弄清楚。

我有一条嵌套路线,如下所示:

  resources :pages, :path => '' do
    # WHAT_DO_I_TYPE_HERE
    resources :products, :except => [:index], :path => '' do
        member do
            post 'broken_link'
        end
    end    
  end

其中未标记的 (#) 行是我试图找出的路线。基本上我的网址看起来像这样:

domain.com/page_name/product_name/(通过Friendly_id),例如domain.com/music/mp3/。

我想使用 URL domain.com/music/edit 编辑有关音乐的页面。

现在,如果我输入: 一个。 # 即什么都没有...将该行留空 --- 我收到错误“无法找到 id=edit 的产品”,这在某种程度上是有道理的。它尝试查找名为 edit 的产品,而不是方法 edit。所以我尝试 b.匹配 '编辑', :controller => '页面', :action => '编辑' --- 这给了我错误“无法找到没有 ID 的页面”,我想这也是有道理的。

那么我应该写什么来告诉路由在检查产品名称之前检查编辑方法呢?

Using: Rails 3.1.1 and Friendly_id for url's.

I think this should be fairly easy but I can't figure it out.

I have a nested route that looks like this:

  resources :pages, :path => '' do
    # WHAT_DO_I_TYPE_HERE
    resources :products, :except => [:index], :path => '' do
        member do
            post 'broken_link'
        end
    end    
  end

Where the unmarked (#) line is the one I am trying to figure out. Basically I have urls that look like this:

domain.com/page_name/product_name/ (through friendly_id's), e.g. domain.com/music/mp3/.

I want to use the url domain.com/music/edit to edit the page about music.

Now, if I type:
a. <blank> # i.e. nothing...leave that line blank
--- I get the error "Couldn't find Product with id=edit" which makes sense in a way. It tries to find a PRODUCT called edit not the method edit. So instead I try
b. match 'edit', :controller => 'pages', :action => 'edit'
--- which gives me the error 'Couldn't find Page without an ID' which, I guess, makes sense as well.

So what should I write to tell the routes to check for the edit method before checking for a product name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-12-21 18:58:06

我以前也遇到过这个问题...
您可以采取一些措施来实现您想要的 url 路由...

首先,使用您那里的代码,我建议您查看一下命令行中的 rake paths 生成的内容与 page_path 或 page_product / Product_page 路径相关的任何内容,并查看它捕获的 url + 它们的命名路由。

如果这不完全是您想要的..那么您可以尝试将:

get '/:id' => 'products#show'

代替您的 # WHAT_DO_I_TYPE_HERE

不过,您可能需要对此施加限制(请参阅底部的链接),以便您可以仍然有您的 /mypage/editmypage/new 等...

您真正想做的是设置一条如下所示的路线:

match '/:page_id/:id' => 'products#show', :as => 'product'

这应该位于上面的某个位置这些路线将可用通过product_show_path。这假设您正在使用标准具有/属于关系。

您可以将 重命名为 => 'product' 部分为您想要的任何内容,

例如: :as => 'public_product'

然后更新您的链接以在需要的地方显示 public_products_path

在您的 products#show 操作中您应该执行以下操作:

@product = Product.find(:id => params[:id], :page_id => params[:page_id])

您还可以考虑对路线施加限制...
我在这里整理了一个要点供我自己参考,可能对您有用。
https://gist.github.com/1908782

另外,我遇​​到了另一个问题的答案也可能对你有帮助... https://stackoverflow.com/a/5443829/308701

希望能有所帮助:D

I have had this problem before ...
There are a few things you can do to achieve the url routing you want ...

Firstly, with the code you have there, I would suggest taking a look at what rake routes in the command line produces for anything relating to page_path or page_product / product_page paths and see what urls it catches + their named routes.

If it's not quite what you want .. then you could try putting:

get '/:id' => 'products#show'

in place of your # WHAT_DO_I_TYPE_HERE

Although, you may need to put a constraint on that (see links at the bottom) so that you can still have your /mypage/edit and mypage/new etc ...

What you really want to do is set up a route that looks like this:

match '/:page_id/:id' => 'products#show', :as => 'product'

This should go somewhere above these routes and will be available via the product_show_path. This assumes you're using a standard has/belongs to relationship.

You can rename the :as => 'product' portion to whatever you want,

eg: :as => 'public_product'

Then update your links to show public_products_path where needed

In your products#show action you should then do:

@product = Product.find(:id => params[:id], :page_id => params[:page_id])

you could also look at putting constraints on the routes ...
I have put together a Gist for my own reference here that might be useful to you.
https://gist.github.com/1908782

Also, I came across this answer to another question which might help you too ... https://stackoverflow.com/a/5443829/308701

Hope that helps in some way :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文