在rails路由中获取正确的ID

时间:2015-07-07 14:53:03

标签: ruby-on-rails ruby routes

当我做rake路线时,

GET       test/:test_id/associated_link(.:format)
GET       test/(.:format)
POST      test/(.:format)
GET       test/new(.:format)
GET       test/:id/edit(.:format)
PATCH     test/:id(.:format)
PUT       test/:id(.:format)
DELETE    test/:id(.:format)

我需要第一个实例是test /:id / associated_link

路由文件类似于

Rails.application.routes.draw do
   resources :years
   resources :mateirals
   resources :people
   resources :jobs
   resources :test do
     get 'associated_links'
   end

   root 'welcome#index'
   resources :welcome, :companies, :positions

3 个答案:

答案 0 :(得分:3)

它应该是会员路线。

   resources :test do
     member do
         get 'associated_links'
     end
   end

答案 1 :(得分:2)

您可以使用member route,可在文档中找到。它看起来像这样:

using System;
using System.Collections.Generic;
using NUnit.Framework ;

    [TestFixture ]
    class Dummy
    {

        private List<String> CallHistory;
        private String mPropValue = "ThisIsIt";

        [SetUp]
        public void Setup()
        {
            CallHistory = new List<string>();
        }

        public String Prop1 
        {
            get
            {
            CallHistory.Add("Prop1 Get");
            return mPropValue;
            }
            set
            {
                CallHistory.Add("Prop1 Set");
                mPropValue = value;
            }
        }

        [Test]
        public void UseSameAs()
        {
            Assert.That(Prop1, Is.SameAs(Prop1));
            Assert.That(String.Join(",", CallHistory.ToArray()), Is.EqualTo("Prop1 Get,Prop1 Get"));

        }

        [Test]
        public void UseaAreSame()
        {
            Assert.AreSame(Prop1, Prop1);
            Assert.That(String.Join(",", CallHistory.ToArray()), Is.EqualTo("Prop1 Get,Prop1 Get"));

        }
}

或者,如果您只有一个成员路线,则可以删除该块:

resources :test do 
  member do
    get 'associated_links'
  end
end

答案 2 :(得分:1)

如果您希望URI模式看起来像.over { z-index: 2; position: top center; text-align: center; margin-top: 20px; } #map-canvas { height: 550px; width: 100%; padding-top: 0px; margin: 0px; } ,则需要更改

test/:id/associated_link

resources :test do
   get 'associated_links'
end

<强> 测试