0
votes

Basic help here but I'm new to Ruby On Rails. Rails version 4.1.7 on Windows 7.

I expect to see different urls created for the links. Code and problem description are below.

Thanks for any help!

-T

Controller:

class DemoController < ApplicationController
    layout false

    def index
    end

    def hello
        #render('hello')
        @array = [1,2,3,4,5]
    end

    def other_hello
        redirect_to(:controller => 'demo', :action => 'index')
    end

    def lynda
        redirect_to("http://lynda.com")
    end
end

routes.rb:

Rails.application.routes.draw do
    root "demo#index"
    #get 'demo/index'
    match ':controller(/:action(:id))', :via => :get
end

index.html.erb('/demo/index'):

<h1>Demo#index</h1>
<p>Hello From Index</p>
<a href="/demo/hello">Hello Page 1</a><br />
<%= link_to( "Hello page 2c", {action: "hello" })%> <br />
<%= link_to("Hello with Parameters", {action:"hello", page: 5, id: 20}) %>

rake routes: output

Prefix Verb URI Pattern Controller#Action root GET / demo#index GET /:controller(/:action(:id))(.:format) :controller#:action

Resulting source from index.html:

<h1>Demo#index</h1>
<p>Hello From Index</p>

<a href="/demo/hello">Hello Page 1</a><br />
<a href="/demo">Hello page 2c</a> <br />
<a href="/demo/hello20?page=5">Hello with Parameters</a>

Problem:

I expect the "Hello page 2c" link to have an href of "/demo/hello".

I also expect the "Hello with Parameters" link to have an href of "/demo/hello/20?page=5"

Not sure what to look at further.

I've tried different ways to format the link_to but they all give the same result.

EX:

<%= link_to( "Hello page 2c", {:action => "hello" })%> <br />
<%= link_to("Hello with Parameters", {:action => "hello", page: 5, id: 20}) %><br />

<%= link_to "Hello page 2c", action: "hello" %> <br />
<%= link_to "Hello with Parameters", action: "hello", page: 5, id: 20 %><br />

<%= link_to( "Hello page 2c", {controller: "demo", action: "hello" })%> <br />
<%= link_to("Hello with Parameters", {controller: "demo", action:"hello", page: 5, id: 20}) %><br />
2

2 Answers

1
votes

You are using rails routes the wrong way. You shouldn't use the catch all route. Write an explicit route for each controller action that is available to user. You can read more about routing here.

-1
votes

Try editing your route rule as follows.

match ':controller(/:action(/:id))', :via => :get

Thanks