Link_to :Action won't call Controller Method -- Rails 4

408 Views Asked by At

I'm trying to figure out why my link_to isn't working how I thought it would. In my application, I have a database of prizes. When a user clicks on the link_to, the controller should access the selected action (in this case, "result1") and perform the action's method on the resultpage view. From testing, it appears as if the link_to is completely ignoring the :action => 'result1' and just defaults to doing the Show method.

Any idea why the link_to is completely ignoring the :action => 'result1'?

First View: views/start/start.html.erb view:

<%=link_to 'result', resultpage_path, :controller => 'resultpage', :action => 'result1', :class => 'btn btn-default'%> 

Resultpage Controller: controllers/resultpage_controller.rb

class ResultpageController < ApplicationController

def index
end

# regardless of what is in the :action =>, the controller always calls Show.
def show
  @prize = Prize.first
  render :index
end

# if i make the :action => 'result1', the controller still calls Show.
def result1
  @prize = Prize.last
  render :index
end

Results View: resultspage/index.html.erb

  <h2><%= @prize.title %></h2>
  <p><%= @prize.description %></p>

Routes:

resources :resultpage do
    get 'index'
    get 'show'
end

resources :prizes
1

There are 1 best solutions below

0
On

Make routes with such content

resources :resulpages do
  get :result1
end

Use such link

= link_to 'result', result1_resultpage_paths

And modify tour controller

class ResultpageController < ApplicationController

And, BTW read this article carefully