【问题标题】:Link_to :Action won't call Controller Method -- Rails 4Link_to :Action 不会调用控制器方法——Rails 4
【发布时间】:2015-01-22 15:39:27
【问题描述】:

我正试图弄清楚为什么我的 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'%> 

结果页控制器: 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

【问题讨论】:

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


    【解决方案1】:

    使用此类内容制作路线

    resources :resulpages do
      get :result1
    end
    

    使用这样的链接

    = link_to 'result', result1_resultpage_paths
    

    并修改游览控制器

    class ResultpageController < ApplicationController
    

    而且,顺便说一句,仔细阅读这篇文章

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多