【问题标题】:link_to(:controller=>"something") helper + Devise doesn't work properlylink_to(:controller=>"something") helper + Devise 无法正常工作
【发布时间】:2011-06-15 17:32:52
【问题描述】:

全部! 也许有人知道这是为什么:

def index_link(object, content = t("#{object.to_s.pluralize}.index"))
  #link_to(content, {:controller=>object.to_s.pluralize}, :scope=>"") if can?(:read, p(object)) #this doesn't work too
  link_to(content, :controller=>"trademarks")
  #link_to(content, trademarks_path) #this do work, but I need to set path from object
end

这样例外:

No route matches {:controller=>"devise/trademarks"}

堆栈:

actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:424:in `raise_routing_error'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:406:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:453:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:481:in `url_for'
actionpack (3.0.3) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.3) lib/action_view/helpers/url_helper.rb:99:in `url_for'
actionpack (3.0.3) lib/action_view/helpers/url_helper.rb:236:in `link_to'
app/helpers/application_helper.rb:53:in `index_link'
app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb___741328535__615736668_0'
actionpack (3.0.3) lib/action_view/template.rb:135:in `send'
actionpack (3.0.3) lib/action_view/template.rb:135:in `render'

以及如何让它发挥作用?

【问题讨论】:

  • “我需要从对象设置路径”是什么意思?
  • 您能从您的控制台说明 rake 路由的输出吗?
  • @Heikki,我需要 index_link 方法才能工作。
  • @ecologic,除了设计页面之外,所有应用程序中的路由都是正确的并且工作正常(设计为 link_to 或类似的东西定义了一些范围,它将设计/添加到我的变量中)
  • 这个方法是在哪里定义的?

标签: ruby-on-rails-3 url-routing helpers


【解决方案1】:

您必须在路径中添加一个斜杠。 例如:

<li><%= link_to "Events" , :controller => "/events", :action => "index" %></li>

来源: https://github.com/plataformatec/devise/issues/471#issue/471/comment/395243

【讨论】:

    【解决方案2】:

    这是 rake 路由的一部分:

          new_user_session GET    /users/login(.:format)         {:controller=>"devise/sessions", :action=>"new"}
              user_session POST   /users/login(.:format)         {:controller=>"devise/sessions", :action=>"create"}
      destroy_user_session GET    /users/logout(.:format)        {:controller=>"devise/sessions", :action=>"destroy"}
             user_password POST   /users/password(.:format)      {:controller=>"devise/passwords", :action=>"create"}
         new_user_password GET    /users/password/new(.:format)  {:controller=>"devise/passwords", :action=>"new"}
        edit_user_password GET    /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
                           PUT    /users/password(.:format)      {:controller=>"devise/passwords", :action=>"update"}
         user_registration POST   /users(.:format)               {:controller=>"devise/registrations", :action=>"create"}
     new_user_registration GET    /users/register(.:format)      {:controller=>"devise/registrations", :action=>"new"}
    edit_user_registration GET    /users/edit(.:format)          {:controller=>"devise/registrations", :action=>"edit"}
                           PUT    /users(.:format)               {:controller=>"devise/registrations", :action=>"update"}
                           DELETE /users(.:format)               {:controller=>"devise/registrations", :action=>"destroy"}
                           GET    /content/:id-:alias(.:format)  {:controller=>"contents", :action=>"show", :id=>/\d+/}
                  contents GET    /content(.:format)             {:controller=>"contents", :action=>"index"}
                           POST   /content(.:format)             {:controller=>"contents", :action=>"create"}
               new_content GET    /content/new(.:format)         {:controller=>"contents", :action=>"new"}
              edit_content GET    /content/:id/edit(.:format)    {:controller=>"contents", :action=>"edit"}
                   content GET    /content/:id(.:format)         {:controller=>"contents", :action=>"show"}
                           PUT    /content/:id(.:format)         {:controller=>"contents", :action=>"update"}
                           DELETE /content/:id(.:format)         {:controller=>"contents", :action=>"destroy"}
                trademarks GET    /trademarks(.:format)          {:controller=>"trademarks", :action=>"index"}
                           POST   /trademarks(.:format)          {:controller=>"trademarks", :action=>"create"}
             new_trademark GET    /trademarks/new(.:format)      {:controller=>"trademarks", :action=>"new"}
            edit_trademark GET    /trademarks/:id/edit(.:format) {:controller=>"trademarks", :action=>"edit"}
                 trademark GET    /trademarks/:id(.:format)      {:controller=>"trademarks", :action=>"show"}
                           PUT    /trademarks/:id(.:format)      {:controller=>"trademarks", :action=>"update"}
                           DELETE /trademarks/:id(.:format)      {:controller=>"trademarks", :action=>"destroy"}
    

    请求的页面是 /users/edit。该错误仅发生在设计控制器中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多