【问题标题】:Rails User-Profile model challengesRails 用户配置文件模型挑战
【发布时间】:2010-05-10 02:45:33
【问题描述】:

我正在尝试创建一个类似于 SO 的注册流程:

  • 路由到 OpenID 提供者
  • provider将用户信息返回给UsersController(猜测)
  • UsersController 创建用户,然后路由到 ProfilesController 的新操作或编辑操作。

目前,我只是尝试创建用户,然后路由到 ProfilesController 的新操作或编辑操作(不确定我应该使用哪个)。

这是我目前所拥有的:

型号:

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

路线:

  map.resources :users do |user|
    user.resource :profile
  end

 new_user_profile GET    /users/:user_id/profile/new(.:format)  {:controller=>"profiles", :action=>"new"}
edit_user_profile GET    /users/:user_id/profile/edit(.:format) {:controller=>"profiles", :action=>"edit"}
     user_profile GET    /users/:user_id/profile(.:format)      {:controller=>"profiles", :action=>"show"}
                  PUT    /users/:user_id/profile(.:format)      {:controller=>"profiles", :action=>"update"}
                  DELETE /users/:user_id/profile(.:format)      {:controller=>"profiles", :action=>"destroy"}
                  POST   /users/:user_id/profile(.:format)      {:controller=>"profiles", :action=>"create"}

    users GET    /users(.:format)                       {:controller=>"users", :action=>"index"}
          POST   /users(.:format)                       {:controller=>"users", :action=>"create"}
 new_user GET    /users/new(.:format)                   {:controller=>"users", :action=>"new"}
edit_user GET    /users/:id/edit(.:format)              {:controller=>"users", :action=>"edit"}
     user GET    /users/:id(.:format)                   {:controller=>"users", :action=>"show"}
          PUT    /users/:id(.:format)                   {:controller=>"users", :action=>"update"}
          DELETE /users/:id(.:format)                   {:controller=>"users", :action=>"destroy"}

控制器:

class UsersController < ApplicationController

  # generate new-user form
  def new
    @user = User.new
  end

  # process new-user-form post
  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to new_user_profile_path(@user)
    ...
    end
  end

  # generate edit-user form
  def edit
    @user = User.find(params[:id])
  end

  # process edit-user-form post
  def update

    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(users_path) }
        format.xml  { head :ok }
      ...
      end
    end

end 

class ProfilesController < ApplicationController

  before_filter :get_user

  def get_user
    @user = User.find(params[:user_id])
  end

  # generate new-profile form
  def new
    @user.profile = Profile.new
    @profile = @user.profile
  end

  # process new-profile-form post
  def create

    @user.profile = Profile.new(params[:profile])
    @profile = @user.profile

    respond_to do |format|
      if @profile.save
        flash[:notice] = 'Profile was successfully created.'
        format.html { redirect_to(@profile) }
        format.xml  { render :xml => @profile, :status => :created, :location => @profile }
      ...
      end
    end

  end

  # generate edit-profile form
  def edit
    @profile = @user.profile
  end

  # generate edit-profile-form post
  def update

    @profile = @user.profile

    respond_to do |format|
      if @profile.update_attributes(params[:profile])
        flash[:notice] = 'Profile was successfully updated.'
        # format.html { redirect_to(@profile) }
        format.html { redirect_to(user_profile(@user)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end

end

编辑-用户视图:

...
<% form_for(@user) do |f| %>
...

新的个人资料视图:

...
<% form_for([@user,@profile])  do |f| %>
..

我有两个问题:

  1. 将编辑保存到 User 模型时,UsersController 会尝试路由到 http://localhost:3000/users/1/profile.%23%3Cprofile:0x10438e3e8%3E,而不是 http://localhost:3000/users/1/profile

  2. 当渲染新的配置文件表单时,它会抛出一个错误,内容为:undefined method `user_profiles_path' for #

  3. 在创建用户时(在 UsersController 中)创建一个空白配置文件是否更好,然后对其进行编辑或遵循在 ProfilesController 中创建配置文件的其余约定(就像我所做的那样)?

我错过了什么?

我确实查看了Associating Two Models in Rails (user and profile),但它并没有满足我的需求。

感谢您的宝贵时间。

【问题讨论】:

    标签: ruby-on-rails model profile


    【解决方案1】:

    我将 Edit-Profile 表单更改为:

    <% form_for([@user,@profile], :url => user_profile_path(@user)) do |f| %>
    

    一切都按预期进行。

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 2012-08-25
      相关资源
      最近更新 更多