【问题标题】:Rails rendering partials - Keep getting undefined method nil:NilClassRails渲染部分 - 不断获得未定义的方法nil:NilClass
【发布时间】:2013-05-02 00:22:38
【问题描述】:

我是编程新手,我正在通过rails 工作。我正在尝试构建小型应用程序,但我很困惑这个问题。我一直在改编 Michael Hartl 教程来构建应用程序,但我想我遗漏了一些东西。

下面我已经分解了代码的相关部分(希望如此),我希望能得到一些帮助来解决问题所在。

错误是:

nil:NilClass 的未定义方法“用户名”

指出我在尝试在主页上显示注册表单部分之前遇到了类似的错误可能会很有用。已通过以下方式修复:

<%= render :partial => 'users/new', :locals => { :user => @user ||= User.new } %>

在这种情况下做同样的事情确实会消除错误,但应用程序的行为就像我已登录但不允许我退出或与任何功能交互。

我尝试了上述的各种变体,包括:@current_user => current_user 等

有人可以解释我做错了什么,因为我似乎做错了什么!

谢谢。

用户控制器

class UsersController < ApplicationController

  before_filter :signed_in_user,  only: [:show]
  before_filter :correct_user,    only: [:show]
  before_filter :admin_user,      only: [:index, :destroy]     

  def index
    @users = User.paginate(page: params[:page])

  end

  def show
    @user = User.includes(:video_posts).find(params[:id])
    @video_posts = @user.video_posts.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render :template => 'static_pages/home'
    end
  end

视频发布控制器

class VideoPostsController < ApplicationController

  before_filter :signed_in_user

  def create

  end

  def destroy

  end


end

会话控制器

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = "Invalid email/password combination"
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end

end

会话助手

module SessionsHelper
 def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def current_user?(user)
    user == current_user
  end
end

STATICPAGES > ROOT TO HOME

<%= render :partial => 'shared/user_info', :locals => { :current_user => current_user } %>

/共享文件夹中的部分内容

<h2><%= current_user.username %></h2>
<span><%= link_to "view profile", current_user %></span>
<span><%= pluralize(current_user.video_posts.count, "video_post") %></span>

【问题讨论】:

    标签: ruby-on-rails render partial


    【解决方案1】:

    您的部分想要显示只有拥有帐户的用户拥有的数据。但是,当用户进入您的主页时,没有 current_user 对象,因为他没有使用 jet 登录。您可以将您的 render :partial 调用包含在以下内容中:

    <% if current_user %>
      <%= render :partial ... %>
    <% else %>
      <h1>Welcome</h1>
    <% end %>
    

    仅当用户登录时才会显示用户信息。

    【讨论】:

    • 转发你的答案我相信这在技术上意味着当他在UserController的索引操作中他一定想要这样的东西:@user = current_user这意味着当index呈现动作它将获得 current_user。他的问题是 [:username] 为 nil,他试图从 nil 对象中检索 id 键的值。
    • 我不太确定你在说什么。我对他的问题的解释是,他在他的部分的第一行得到了异常,因为 current_user 是 nil,因为他的 cookie 中没有 remember_token。我不确定这是正确的解释,但我也看不出涉及 UsersController#index。
    • 丹尼尔你是对的。非常感谢!这确实对事情有所启发,但问题仍然存在。我应该如何渲染局部?
    • 在您的示例中,您使用局部变量 current_user,因此您完全正确地调用它:render :partial => 'whatever', :locals => { :current_user => 'your value' }跨度>
    • 我明白了。我想部分渲染可能会中断的原因有一百万个。还是有些不对劲。尝试做 'shared/video_post_form', :@video_posts => video_posts %> 来渲染 等给了我一个无方法错误。如果你能解释为什么会很棒吗?谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多