【问题标题】:Why isn't this link to tag linking to the correct model?为什么这个标签链接没有链接到正确的模型?
【发布时间】:2011-03-24 02:12:48
【问题描述】:

所以在拉了一个小时的头发后,我决定在这里发布这个。我收到此错误:

NoMethodError in Articles#show

Showing /home/leon/sites/VIS/app/views/articles/_article.html.erb where line #3 raised:

undefined method `user_path' for #<#<Class:0x7fdd81fe1eb8>:0x7fdd81fdfdc0>
Extracted source (around line #3):

1: <div id="left-column">
2:  <p class="label-b">Author<br>
3:     <%= link_to "#{@article.user.penname}'s profile", user_path(article.user) %> 

user_path(article.user) %>

当我尝试链接到文章的创作用户时。我的路线文件:

  resources :tags

  get "admin/index"
  get "admin/show"
  get "articles/contact"
  get "articles/about"

  resources :roles

  devise_for :users, :controllers => { :registrations => "users/registrations" }

  resources :articles do
    resources :comments
  end

我的文章模型文件中有一个 belongs_to :user ,我的用户模型文件中有一个 has_many :articles 。我正在使用此链接链接到文章中作者的个人资料。

请帮忙!我正在使用 can can 进行权限管理并设计用于身份验证,但是它在文章的显示操作中引发了错误,因此我没有发布该代码。让我知道我是否应该这样做。谢谢!

@雅各布

我得到同样的错误:

文章中的NoMethodError#show

显示第 3 行出现的 /home/leon/sites/VIS/app/views/articles/_article.html.erb:

# 的未定义方法 `user_path' 提取的源代码(第 3 行附近):

1: 2:作者
3: 4:以前的版本 5:版本 7

6: "favorites-button" %>

我的路线的相关部分:

{:action=>"destroy", :controller=>"roles"}
      new_user_session GET    /users/sign_in(.:format)                          {:action=>"new", :controller=>"devise/sessions"}
          user_session POST   /users/sign_in(.:format)                          {:action=>"create", :controller=>"devise/sessions"}
  destroy_user_session GET    /users/sign_out(.:format)                         {:action=>"destroy", :controller=>"devise/sessions"}
         user_password POST   /users/password(.:format)                         {:action=>"create", :controller=>"devise/passwords"}
     new_user_password GET    /users/password/new(.:format)                     {:action=>"new", :controller=>"devise/passwords"}
    edit_user_password GET    /users/password/edit(.:format)                    {:action=>"edit", :controller=>"devise/passwords"}
         user_password PUT    /users/password(.:format)                         {:action=>"update", :controller=>"devise/passwords"}
     user_registration POST   /users(.:format)                                  {:action=>"create", :controller=>"users/registrations"}
 new_user_registration GET    /users/sign_up(.:format)                          {:action=>"new", :controller=>"users/registrations"}
edit_user_registration GET    /users/edit(.:format)                             {:action=>"edit", :controller=>"users/registrations"}
     user_registration PUT    /users(.:format)                                  {:action=>"update", :controller=>"users/registrations"}
     user_registration DELETE /users(.:format)                                  {:action=>"destroy", :controller=>"users/registrations"}
     user_confirmation POST   /users/confirmation(.:format)                     {:action=>"create", :controller=>"devise/confirmations"}
 new_user_confirmation GET    /users/confirmation/new(.:format)                 {:action=>"new", :controller=>"devise/confirmations"}
     user_confirmation GET    /users/confirmation(.:format)                     {:action=>"show", :controller=>"devise/confirmations"}

啊哈!它没有用于 user#show 的路线,但我不知道为什么,因为我有:

class UsersController < ApplicationController
...
  def show
    respond_to do |format|
      format.json { render :json => @user }
      format.xml  { render :xml => @user }
      format.html
    end

  rescue ActiveRecord::RecordNotFound
    respond_to_not_found(:json, :xml, :html)
  end

在我的应用程序/控制器文件夹中。我是否需要将我的用户控制器移动到 app/controllers/devise 并使用 devise 而不是应用程序控制器重载它?

【问题讨论】:

    标签: ruby-on-rails rails-models


    【解决方案1】:

    你可以这样做:

     <%= link_to "#{@article.user.penname}'s profile", article.user %> 
    

    Rails 会自动确定路线。

    如下所示:

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

    更新

    是的,您将不得不从 DeviseController 而不是 ApplicationController 继承。从这里您可以添加自己的自定义操作,例如 show(这就是您现在所做的)。

    看:https://github.com/plataformatec/devise

    并搜索:配置控制器

    【讨论】:

      【解决方案2】:

      尝试article.user 而不是user_path(article.user)。如果这不起作用,您可以发布rake routes 的输出吗?

      【讨论】:

        【解决方案3】:

        我想通了。在玩了一段时间的路线并阅读了 rails 文档之后,我将这一行添加到我的路线文件中:

        devise_for :users, :controllers => { :registrations => "users/registrations" }

        资源 :users, :only => [:index, :show]

        在我的 devise_for 路线下方。这使我可以显示用户配置文件,而不必担心设计提供的编辑更新等角色。无论如何,感谢您的回答,我很感激!

        【讨论】:

        • 出于好奇,您应该使用@article.user 吗?您在同一个 link_to 中使用了两个不同的变量。
        • 是的,我对 Rails 有点陌生,我最初发布它时不确定是否是。
        【解决方案4】:

        不应该只是:@article.user 而不是 article.user 吗??

        <%= link_to "#{@article.user.penname}'s profile", @article.user %>
        

        为什么要为用户笔名使用实例变量,为用户对象使用局部变量?我认为这是你的问题。

        【讨论】:

        • 不,主要问题是当我执行 rake 路由时,根本没有路由 user#show 操作,这就是错误所在。我还将文章改回@article,但这只是我在弄清楚之前尝试使它工作的众多事情之一。
        猜你喜欢
        • 2018-03-25
        • 1970-01-01
        • 2019-02-19
        • 2022-07-07
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多