gpt4 book ai didi

ruby-on-rails - Rails User-Profile 模型挑战

转载 作者:行者123 更新时间:2023-12-05 01:11:41 25 4
gpt4 key购买 nike

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

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

  • 现在,我只是尝试创建用户,然后路由到 ProfilesController 的 new 或 edit 操作(不确定我应该使用哪个)。

    这是我到目前为止所拥有的:

    楷模:
    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"}

    Controller :
    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

    编辑用户 View :
    ...
    <% form_for(@user) do |f| %>
    ...

    新配置文件 View :
    ...
    <% form_for([@user,@profile]) do |f| %>
    ..

    我有两个问题:
  • 将编辑保存到 User 模型时,UsersController 尝试路由到 http://localhost:3000/users/1/profile.%23%3Cprofile:0x10438e3e8%3E , 而不是 http://localhost:3000/users/1/profile
  • 渲染新配置文件表单时,它会抛出一个错误,内容如下: undefined method `user_profiles_path' for #
  • 在创建用户时(在 UsersController 中)创建一个空白配置文件,然后编辑它或遵循在 ProfilesController 中创建配置文件的其余约定(正如我所做的那样)是否更好?

  • 我错过了什么?

    我做过评论 Associating Two Models in Rails (user and profile) ,但它没有满足我的需求。

    谢谢你的时间。

    最佳答案

    我将 Edit-Profile 表单更改为:

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

    一切都按预期进行。

    关于ruby-on-rails - Rails User-Profile 模型挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2800052/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com