2011-09-06 23 views
1

我幾乎與將註冊過程完成:問題編輯/填寫表格爲新創建的簡檔

  • (步驟1)新用戶完成一個小的形狀,既創建一個用戶和簡介
  • (步驟2)新用戶被重定向到ProfilesController填寫的個人資料
  • (步驟3)新用戶完成檔案的休息,會被重定向到他們的個人資料

丈二和尚​​不工作對。我相信我所做的是在上面的步驟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(配置文件#新)形式:

<%= 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" 

我得到

rake routes 「沒有ID找不到用戶」:

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和r OOT。

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"} 

回答

1

在用戶模型中是否使用了accep_nested_attributes_for?至少,它可能會清理你的代碼。但是,無論如何,我相信問題出在您的路線上。除了閱讀這段代碼,我建議你在你的應用程序根目錄下運行一個「rake routes」,以確保你嘗試使用的路線確實存在。

更新:對於您特定的代碼庫,以下是您需要執行的操作。

使用在你的routes.rb這些路線:

resources :profiles 
resources :users 
root :to => "users#new" 

你也需要改變:

redirect_to profile_pathredirect_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 

控制器S/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塊藥粥輪廓的唯一形式值。我認爲你所要做的並不太複雜,但我認爲你的路線選擇可能會出錯。

乾杯

+0

謝謝!我在我的模型中使用了accep_nested_attributes_for。我有兩種形式,一種是嵌套模型形式(創建用戶和配置文件),另一種僅用於配置文件。路線問題必須發生在用戶和配置文件以第一種形式創建之後,然後被重定向到填寫/編輯剛剛創建的配置文件。 – tvalent2

+0

我是一名編程初學者,但顯然我需要重新理解路線。 – tvalent2

+0

你可以編輯你的問題以包含'rake routes'的信息嗎?您只需要包含配置文件和用戶的行。 –