【问题标题】:How do I access a Profile model insider users_controller in ruby on rails?如何在 ruby​​ on rails 中访问 Profile 模型内部 users_controller?
【发布时间】:2012-01-05 23:11:52
【问题描述】:

我想从属于_to :user "User model" 的Profile 模型中访问“profiles”表。

首先,在 users_controller 的显示操作中,我想从配置文件表中获取数据,以便在用户配置文件页面上显示。

其次,我想让用户可以使用表单来编辑这些内容。我知道这是在更新操作中完成的吗?而编辑操作可以在编辑视图页面上显示表单..

这是我的控制器:

class UsersController < ApplicationController



  def new
    @user = User.new 
    @title = "Practice"
  end

  def create
    @user = User.new(params[:user])   
    respond_to do |format|
      if @user.save 
        @user.build_profile.save #same as Profile.new(:user_id => @user.id)
        login @user
        UserMailer.join_confirmation(@user).deliver
        format.js   { render :js => "window.location = '#{root_path}'" } 
        flash[:notice] = "Welcome!"
      else

        format.js   { render :form_errors }
      end
    end
  end

  def show

  end

  def update

  end

  def edit

  end



end

1) 我如何访问我的个人资料表? 用户 has_one :profile 个人资料belongs_to :user

建议将不胜感激。我花了半天的时间来弄清楚如何在配置文件表中创建与新创建的用户对应的行,现在下一步是能够从我的 users_controller 中的模型中获取数据。我知道我可以创建一个profiles_controller 并在那里做一些事情,但我现在不想尝试,因为我确信有办法通过users_controller 来做。

提前感谢您提供的建议。

查看:

<%= @profile_data.first_name %>

<h4><%= current_user.username.capitalize %></h4>
<%= gravatar_image_tag(current_user.email, :alt => @title, :class => "gravatar", :gravatar => { :size => 150 }) %>
<br />

试图通过 users_controller 从配置文件表中提取 first_name

  def show
    @user = User.find_by_username(params[:username])
    @profile_data = @user.profile

  end

路线:

  match ':username' => "users#show"

当我访问 localhost:3000/username 时,我希望看到存储在配置文件表的 first_name 列中的名称

它不显示用户的名字。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 rubygems ruby-on-rails-3.1


    【解决方案1】:

    你可以这样做:

    @user.profile
    

    这将返回属于用户的个人资料

    否则:

    Profile.where('whatever condition you fancy')
    

    将根据条件返回一个配置文件对象

    【讨论】:

    • 我收到此错误:nil:NilClass 的未定义方法 `profile'
    • 表示用户为空。你需要填充它。可能使用类似:@user = User.find(params[:id]) 但如果没有看到路线和正在运行的操作就无法告诉您更多信息
    • 我在配置文件表中有一些数据。我在 localhost:3000/username 有一个用户配置文件,这里是“匹配 ':username' => "users#show"" 的路线现在我想要做的是在用户页面上显示该用户的数据我参观它。所以在显示视图中我有@variable_set_in_controller.first_name 来查看它是否可以获取名字。在表演动作中,我已经尝试了一切,但仍然没有运气。我只想能够从配置文件表/配置文件模型中获取信息到 users_controller 的显示操作中
    • I the show action 你需要设置@user 所以你的show action 看起来像def show @user = User.find_by_username(params[:username]) @profile = @user.profile 路由显示传递的参数是用户名,所以你需要使用它来查找用户对象
    • 我将这些内容添加到我的展示操作中,然后尝试从展示视图调用年龄加载但它没有提取数据。因此,在节目中,我将 [at]profile.first_name 更改为我知道不存在的其他内容。我收到未定义方法的错误,因此我将其设置回 [at]profile.first_name 并且错误很好,因此它似乎可以找到数据,但由于某种原因它不会显示它。
    【解决方案2】:

    您需要在 User 模型上使用 has_one:profile,在 Profile 模型上使用 belongs_to:user,确保配置文件表中有适当的 user_id 列。将accepts_nested_attributes_for:profile 添加到 User 模型中,然后您可以在编辑用户的相同表单中创建/编辑用户的配置文件。

    阅读轨道 release notes on nested attributes 和指南:associationsforms

    【讨论】:

    • 不走运,我尝试在显示操作中使用用户名查找,但由于某种原因,我的日志告诉我它正在查找“favicon”作为用户名。没有意义。
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多