我几乎完成了一个注册过程,其中:
然而有些事情是行不通的。我相信我所做的就是在上面的步骤1和2中创建一个配置文件。 (类似于this post,但我的方法不同。)
任何人都可以帮我解决这个问题吗?
这是我的代码。
ProfilesController:
def new
@profile = Profile.new(params[:profile])
end
def show
@user = User.find(params[:id])
@profile = @user.profile
@superlative = @profile.superlatives.new
end
def edit
@profile = user.profile
end
UsersController:
def new
@user = User.new
@user.profile = Profile.new
if logged_in?
redirect_to current_user.profile
end
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
redirect_to signup_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
def edit
@user = current_user
end
def update
@user = current_user
if @user.update_attributes(params[:email])
redirect_to profile_path
else
render :action => 'edit'
end
end
routes.rb中:
match "/signup" => "profiles#edit", :as => "signup"
post "/profiles/new" => "profiles#create"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
get "/profiles/:id" => "profiles#show", :as => "profile"
get "profiles/new"
root :to => "users#new"
resources :users do
resources :profiles
end
表单#1(用户#新表单):
<%= form_for(@user, :html => {:multipart => true, :id => 'homesign'}) do |f| %>
<%= f.hidden_field :id %>
<%= f.label :email %>
<%= f.text_field :email, :size => 38 %>
<%= f.fields_for :profile do |profile| %>
<div id="name">
<%= profile.label :first_name %>
<%= profile.text_field :first_name, :size => 18 %>
</div>
<% end %>
表格#2(个人资料#new)表格:
<%= form_for '@user.profile', :html => { :multipart => true } do |f| %>
<%= f.hidden_field :id %>
<table id="signupTable">
<tbody>
<tr>
<td class="label"><%= f.label :gender, "Gender:" %></td>
</tr>
<tbody>
</table>
<% end %>
如果我使用上述内容,我会收到“无法找到ID = xx的用户”
如果我改变上述路线:
match "/signup" => "profiles#new", :as => "signup"
要:
match "/signup" => "profiles#edit", :as => "signup"
我收到“找不到没有ID的用户”
来自rake routes
:
profiles_show GET /profiles/show(.:format) {:action=>"show", :controller=>"profiles"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
profiles_new GET /profiles/new(.:format) {:action=>"new", :controller=>"profiles"}
POST /profiles/new(.:format) {:action=>"create", :controller=>"profiles"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
user_profiles GET /users/:user_id/profiles(.:format) {:action=>"index", :controller=>"profiles"}
POST /users/:user_id/profiles(.:format) {:action=>"create", :controller=>"profiles"}
new_user_profile GET /users/:user_id/profiles/new(.:format) {:action=>"new", :controller=>"profiles"}
edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
user_profile GET /users/:user_id/profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
PUT /users/:user_id/profiles/:id(.:format) {:action=>"update", :controller=>"profiles"}
DELETE /users/:user_id/profiles/:id(.:format) {:action=>"destroy", :controller=>"profiles"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
我还有以下注册路线:
signup_index GET /signup(.:format) {:action=>"index", :controller=>"signup"}
POST /signup(.:format) {:action=>"create", :controller=>"signup"}
new_signup GET /signup/new(.:format) {:action=>"new", :controller=>"signup"}
edit_signup GET /signup/:id/edit(.:format) {:action=>"edit", :controller=>"signup"}
GET /signup/:id(.:format) {:action=>"show", :controller=>"signup"}
PUT /signup/:id(.:format) {:action=>"update", :controller=>"signup"}
DELETE /signup/:id(.:format) {:action=>"destroy", :controller=>"signup"}
更新:rake routes
只有resources: users
,resources :profiles
和root。
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
profiles GET /profiles(.:format) {:action=>"index", :controller=>"profiles"}
POST /profiles(.:format) {:action=>"create", :controller=>"profiles"}
new_profile GET /profiles/new(.:format) {:action=>"new", :controller=>"profiles"}
edit_profile GET /profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
PUT /profiles/:id(.:format) {:action=>"update", :controller=>"profiles"}
DELETE /profiles/:id(.:format) {:action=>"destroy", :controller=>"profiles"}
root /(.:format) {:action=>"new", :controller=>"users"}
答案 0 :(得分:1)
您是否在用户模型中使用accepts_nested_attributes_for?至少,它可能会清理你的代码。但是,无论如何,我认为问题出在你的路线上。除了阅读此代码之外,我建议您在应用程序根目录中运行“rake路由”,以确保您尝试使用的路由确实存在。
更新:对于您的特定代码库,您需要执行以下操作。
在routes.rb中使用这些路线:
resources :profiles
resources :users
root :to => "users#new"
您还需要更改:
redirect_to profile_path
至redirect_to @user.profile
redirect_to signup_path ...
至redirect_to edit_profile_path(@user.profile)
以下是我通常对用户和个人资料执行的操作,可能有所帮助:
支架(仅供上下文使用):
rails generate scaffold User username:string password:string
rails generate scaffold Profile user_id:integer email_address:string website:string
<强> routes.rb中:强>
resources :profiles
resources :users
<强>控制器/ users_controller.rb:强>
class UsersController < ApplicationController
...
# GET /users/new
def new
@user = User.new
@profile = Profile.new
respond_to do |format|
format.html
format.xml { render :xml => @user }
end
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
@profile = @user.profile.nil? ? Profile.new : @user.profile
respond_to do |format|
format.html
format.xml { render :xml => @user }
end
end
# POST /users
def create
if params[:user][:profile]
profile = params[:venue][:profile]
params[:user].delete(:profile)
params[:user].update({ :profile_attributes => profile })
end
@user = User.find(params[:id])
respond_to do |format|
if @user.save
format.html { redirect_to :action => (@user.profile.nil? ? @user : @user.profile) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
def update
if params[:user][:profile]
profile = params[:venue][:profile]
params[:user].delete(:profile)
params[:user].update({ :profile_attributes => profile })
end
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to :action => (@user.profile.nil? ? @user : @user.profile) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
...
end
<强>模型/ profile.rb:强>
class Profile < ActiveRecord::Base
belongs_to :user
end
<强>模型/ user.rb:强>
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
用户/ _form.html.erb:强>
<%= form_for(@user) do |f| %>
...
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<%= f.fields_for @profile, :profile do |ff| %>
<div class="field">
<%= ff.label :email_address %><br />
<%= ff.text_field :email_address %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
使用这种流程,有人可以创建用户和配置文件,并且要填写的配置文件的唯一表单值是您指定的默认值或出现在fields_for块中的默认值。我认为你试图做的并不是过于复杂,但我认为你的路由可能会以错误的方式接近。
干杯