Rails 友好的 Url 如何防止重复?和路由

发布于 2024-10-21 18:09:57 字数 1062 浏览 8 评论 0原文

我正在使用友好的网址。

当我访问 http://localhost:3000/9 时,我遇到了这个问题,它显示与 http://localhost:3000/vind-rejse

我的类别 vind-rejse 的 ID 为 9

我如何获取Rails 只响应 http://localhost:3000/vind-rejse

我如何链接到 konkurrancers 显示操作?

我的类别控制器:

 def show
    @kategori = Kategori.find(params[:id])
    @konkurrancer = @kategori.konkurrancers.find(:all)

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @kategori }
    end
  end

我的 Konkurrancers 控制器:

  def show
    @konkurrancer = Konkurrancer.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @konkurrancer }
    end
  end

我的路线:

match ':kategoris/:id' => 'konkurrancers#show'
match '/:id' => 'kategoris#show'

I am using friendly url.

I am using that problem when I visit http://localhost:3000/9 it shows the same page as http://localhost:3000/vind-rejse

My Category vind-rejse have the ID of 9

How do I get rails only to respond on http://localhost:3000/vind-rejse?

And how do I link to konkurrancers show action?

My Kategoris controller:

 def show
    @kategori = Kategori.find(params[:id])
    @konkurrancer = @kategori.konkurrancers.find(:all)

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @kategori }
    end
  end

My Konkurrancers controller:

  def show
    @konkurrancer = Konkurrancer.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @konkurrancer }
    end
  end

My routes:

match ':kategoris/:id' => 'konkurrancers#show'
match '/:id' => 'kategoris#show'

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

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

发布评论

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

评论(1

夜光 2024-10-28 18:09:57

这样的事情会起作用:

@kategori = Kategori.find_by_name!(params[:id])

并且适用于您的路线

match ':kategoris/:id' => 'konkurrancers#show', :as => 'whatever'

和您的视图

<%= link_to "Whatever", whatever_path(9) %>

Something like this will work:

@kategori = Kategori.find_by_name!(params[:id])

and for your routes

match ':kategoris/:id' => 'konkurrancers#show', :as => 'whatever'

and in your views

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