我幾乎與將註冊過程完成:問題編輯/填寫表格爲新創建的簡檔
- (步驟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"}
謝謝!我在我的模型中使用了accep_nested_attributes_for。我有兩種形式,一種是嵌套模型形式(創建用戶和配置文件),另一種僅用於配置文件。路線問題必須發生在用戶和配置文件以第一種形式創建之後,然後被重定向到填寫/編輯剛剛創建的配置文件。 – tvalent2
我是一名編程初學者,但顯然我需要重新理解路線。 – tvalent2
你可以編輯你的問題以包含'rake routes'的信息嗎?您只需要包含配置文件和用戶的行。 –