1
votes

The "a href" works as planned but the "Link_to" is adding an "id" to the end of the "hello" action directing me to demo/helloid instead of demo/hello. See Rails .erb code below

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

<a href="/demo/hello">Hello page 1</a><br />

<%= link_to "Hello Page 2", ({ controller: "demo", action: "hello"}) %>

While looking in at the HTML source it presents the following

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

<a href="/demo/hello">Hello page 1</a><br />

<a href="/demo/helloid">Hello Page 2</a>

Routes

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

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

end
1
could you provide routes.rb and controller code?Kkulikovskis
please post your routes fileAmit Sharma
also have tried something like this eg. link_to "Profile", { controller: "profiles", action: "show", id: @profile}Amit Sharma
I edited the controller and routes file inJeff A
I'm confused what the actual question is. What is it doing versus what are you expecting it to do?Sia

1 Answers

0
votes

Your route is incorrect on the following line:

match ':controller(/:action(id))',   :via => :get   # Incorrect AND unnecessary

Additionally, this route does not even need to exist because Rails can do this for you using the url_for method:

<%= link_to "Hello Page 2", url_for(controller: "demo", action: "hello") %>

As long as there is a route with the appropriate controller/action assigned in routes.rb, this will generate the URL for that specific route.

> Read the Rails url_for Docs here