【发布时间】: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