1

看过一些参考资料,但它是旧的 Rails 2 解决方案。很难理解一些 Rails 3 命名法。

我在控制器中添加了一个方法“dndl”。我在我的索引中添加了一个 link_to。我试了又试,把路线放进去。

Controller:
def dnld
  blah blah
end

指数:

<td><%= link_to 'Show', stock %></td>
<td><%= link_to 'Edit', edit_stock_path(stock) %></td>
<td><%= link_to 'Dnld', dnld, {:action => 'dnld'} %></td>
<td><%= link_to 'Destroy', stock, :confirm => 'Are you sure?', :method => :delete %>

路线:

  resources :stocks do
    collection do
        put 'dnld'
    end
  end

我试过了:

    <td><%= link_to 'Dnld', stock, {:action => 'dnld'} %></td>
# End up on the stock show page with dnld not executed to my knowledge

    <td><%= link_to 'Dnld', , {:action => 'dnld'} %></td>
# Produces an error

    <td><%= link_to 'Dnld', dnld_stock_path(stock), {:action => 'dnld'} %></td>
# It doesn't know what dnld_stock_path is, yet I don't understand why it DOES know what edit_stock_path is and cannot find documentation to explain this.

谢谢您的帮助!

4

2 回答 2

1

尝试

 resources :stocks do
    collection do
        put :dnld, :as => dnld
    end
  end

然后

<%= link_to "Dnld", dnld_controllername_path %>

于 2011-04-03T14:57:35.333 回答
1

如果您在您的 routes.rb 中提供集合,例如:

  resources :stocks do
    collection do
        put 'dnld'
    end
  end

那么命名路径将是“dnld_stocks_path”。而且您不需要指定操作。

<td><%= link_to 'Dnld', dnld_stocks_path %></td>

如果您在您的 routes.rb 中提供成员,例如:

  resources :stocks do
    member do
        put 'dnld'
    end
  end

那么命名路径将是“dnld_stock_path(stock)”。

<td><%= link_to 'Dnld', dnld_stock_path(stock) %></td>

欲了解更多信息,请访问

于 2011-04-03T15:02:41.053 回答