关联两种型号的Rails(用户和个人资料)两种、型号、个人资料、用户

2023-09-09 22:06:57 作者:酒蚀离心人

我是新来的Rails。我建立一个应用程序,有一个用户模式和剖面模型。

I'm new to Rails. I'm building an app that has a user model and a profile model.

我想这些模型,使得相关联: - 在用户创建一个账户,就等于自动发送到创建配置文件页面,他创建配置文件连接到只有特定的用户。 - 只有谁拥有该配置文件,用户可以修改。

I want to associate these models such that: - After the user creates an account, he is automatically sent to the "create profile" page, and the profile he creates is connected to only that particular user. - Only the user who owns the profile can edit it.

我用nifty_generators生成的用户模型。当用户点击提交帐户创建,我重定向他的新的配置文件视图中创建一个配置文件。我通过编辑重定向路径在用户控制器这样做。用户控制器看起来是这样的:

I generated the user model using nifty_generators. When the user hits submit for the account creation, I redirect him to the "new profile" view to create a profile. I did this by editing the redirect path in the user controller. The user controller looks like this:

def create
  @user = User.new(params[:user])
  if @user.save
    session[:user_id] = @user.id
    flash[:notice] = "Thank you for signing up! You are now logged in."
    redirect_to new_profile_path
  else
    render :action => 'new'
  end
end

这是工作的,但问题是,应用程序似乎没有认识到,资料被连接到特定的用户。我能够创建配置文件,但是似乎并没有成为配置和用户之间的关系。

This was working, but the problem was that the app didn't seem to recognize that the profile was connected to that particular user. I was able to create profiles, but there didn't seem to be a relationship between the profile and the user.

我的个人资料型号列表:belongs_to的:用户 我的用户模式列表:已_ONE:简介

My Profile model lists: belongs_to :user My User model lists: has _one :profile

我routes.rb中文件中列出了以下内容:      map.resources:用户:HAS_ONE =>:配置文件      map.resources:配置文件

My routes.rb file lists the following: map.resources :users, :has_one => :profile map.resources :profiles

我在配置文件表中的user_id外键。我的模式是这样的:

I have a user_id foreign key in the profiles table. My schema looks like this:

create_table "profiles", :force => true do |t|
  t.integer  "user_id"
  t.string   "name"
  t.string   "address1"
  t.string   "address2"
  t.string   "city"
  t.string   "state"
  t.string   "zip"
  t.string   "phone"
  t.string   "email"
  t.string   "website"
  t.text     "description"
  t.string   "category"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "users", :force => true do |t|
  t.string   "username"
  t.string   "email"
  t.string   "password_hash"
  t.string   "password_salt"
  t.datetime "created_at"
  t.datetime "updated_at"
end

要尝试配置文件连接到用户,我更新了以下内容,这是我从Rails的基本推断入门指南profiles_controller.rb文件。我的想法是,在我的应用程序,配置文件连接到用户以同样的方式,在Rails的入门应用指南,评论连接到岗位。下面是我的配置控制器的相关部分。我可以提供整个事情,如果它可以帮助:

To try to connect the profile to the user, I updated the profiles_controller.rb file with the following, which I basically extrapolated from the Rails Getting Started Guide. My thinking is that in my app, profiles connect to users in the same way that in the Rails Getting Started app, comments connect to posts. Here's the relevant parts of my profiles controller. I can provide the whole thing if it will help:

def new
  @user = User.find(params[:user_id])
  @profile = @user.profile.build
end

def create
  @user = User.find(params[:user_id])
  @profile = @user.profile.build(params[:profile])
  if @profile.save
    flash[:notice] = 'Profile was successfully created.'
    redirect_to(@profile)
  else
    flash[:notice] = 'Error.  Something went wrong.'
    render :action => "new"
  end

结束

让这些更新的配置文件控制器后,现在当我提交的帐户创建屏幕上,我重定向到一个错误页,上面写着:

After making these updates to the profiles controller, now when I submit on the account creation screen, I'm redirected to an error page that says:

的ActiveRecord :: RecordNotFound在ProfilesController#新 找不到用户没有ID

ActiveRecord::RecordNotFound in ProfilesController#new Couldn't find User without an ID

这一切似乎像pretty的直接的Rails的使用情况,但我不知道哪颗是错误的。在此先感谢您的帮助!

This all seems like a pretty straight-forward Rails use case, but I'm not sure which pieces are wrong. Thanks in advance for your help!

推荐答案

在创建用户,客户端重定向到 ProfileController可没有 ID 。您需要显式地传递用户的 ID 中的参数。这是一个成语来引用传递给整个对象,而不仅仅是ID。

When a user is created, the client is redirected to the new action in the ProfileController without an id. You need to explictly pass the user's id in the parameters. It's an idiom to pass the reference to the entire object, not just the id.

# app/controllers/users_controller.rb

def create
  # ...
  redirect_to new_user_profile_path(:user_id => @user)
  # ...
end

请注意,我用 new_user_profile_path ,而不是 new_profile_path 。前者是您在 routes.rb中定义的嵌套的资源。您应该删除 map.resources:配置文件,因为一个配置文件不能没有用户存在

Notice that I'm using new_user_profile_path and not new_profile_path. The former is the nested resource that you defined in routes.rb. You should delete map.resources :profiles because a profile cannot exist without a user.

 
精彩推荐
图片推荐