Link_to:Action不会调用Controller Method - Rails 4

时间:2014-11-24 19:56:40

标签: ruby-on-rails view controller routes link-to

我试图找出为什么我的link_to没有按照我的想法行事。在我的应用程序中,我有一个奖品数据库。当用户单击link_to时,控制器应该访问所选操作(在这种情况下," result1")并在结果页面视图上执行操作方法。从测试开始,似乎link_to完全忽略了:action => ' RESULT1'并且只是默认使用Show方法。

知道为什么link_to完全忽略:action => ' RESULT1'吗

第一次观看: views / start / start.html.erb查看:

<%=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

结果视图: resultspage / index.html.erb

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

路线:

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

resources :prizes

1 个答案:

答案 0 :(得分:0)

制作包含此类内容的路线

resources :resulpages do
  get :result1
end

使用此类链接

= link_to 'result', result1_resultpage_paths

修改巡视控制器

class ResultpageController < ApplicationController

而且,BTW仔细阅读了这篇文章