我正在尝试创建类似于SO的注册流程:
现在,我只是尝试创建用户,然后路由到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| %>
...
New-Profile View:
...
<% form_for([@user,@profile]) do |f| %>
..
我遇到两个问题:
将编辑保存到用户模型时,UsersController会尝试路由到http://localhost:3000/users/1/profile.%23%3Cprofile:0x10438e3e8%3E,而不是http://localhost:3000/users/1/profile
当渲染新的配置文件表单时,它会抛出一个错误,内容为:
在创建用户时(在UsersController中)创建空白配置文件是否更好,然后编辑它或遵循在ProfilesController中创建配置文件的其余约定(正如我所做的那样)? / p>
我错过了什么?
我检查了Associating Two Models in Rails (user and profile),但它没有满足我的需求。
感谢您的时间。
答案 0 :(得分:2)
我将编辑配置文件表单更改为:
<% form_for([@user,@profile], :url => user_profile_path(@user)) do |f| %>
一切都按预期工作。