这个错误告诉我 - 未定义的方法'user_profiles_path',而我的路线就像'user_profile_path'。Profile 是用户的单例子资源。不知道是什么导致了这个错误。该错误由<%= form_for [@user, @profile] do |f| %>
_form.html.erb 引发。
路线.rb:
devise_for :users, :path_names => { :sign_in => "login", :sign_up => "register" } do
get "/login", :to => "devise/sessions#new"
get "/register", :to => "devise/registrations#new"
get "/logout", :to => "devise/sessions#destroy"
get '/account' => 'devise/registrations#edit'
end
root :to => "questions#redirect_on_visit"
match 'home', :to => "questions#index"
resources :questions do
resources :question_responses
end
resources :users do
resource :profile
end
_form.html.erb:
<%= form_for [@user, @profile] do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :display_name, "Display Name" %><br />
<%= f.text_field :display_name, :size => "43" %><br />
</div>
<div class="field">
<%= f.label :current_location, "Current Location" %><br />
<%= f.text_field :current_location, :size => "43" %><br />
</div>
<div><%= f.label :nationality, "Nationality" %><br />
<%= f.collection_select :nationality, Profile::NATIONALITY, :include_blank => true %>
</div><br />
<div><%= f.label :home_place, "Home Place" %><br />
<%= f.collection_select :home_place, Profile::HOME_PLACE, :include_blank => true %>
</div><br />
<div><%= f.label :occupation, "Occupation" %><br />
<%= f.collection_select :occupation, Profile::OCCUPATION, :include_blank => true %>
</div><br />
<div><%= f.label :interest, "Interests" %><br />
<%= f.collection_select :interest, Profile::INTERESTS, :include_blank => true %>
</div><br />
<div><%= f.label :hobby, "Hobbies" %><br />
<%= f.collection_select :hobby, Profile::HOBBIES, :include_blank => true %>
</div><br />
<div class="field">
<%= f.label :bio, "Short Bio" %><br />
<%= f.text_area :bio, :size => "50x5" %>
</div>
<div class="submit">
<%= f.submit "Create Profile" %>
</div>
<% end %>
profile_controller.rb:
class ProfilesController < ApplicationController
before_filter :find_user
def new
@profile = @user.build_profile
end
def edit
end
private
def find_user
@user = User.find(params[:user_id])
end
end
应用程序.html.erb:
<% if user_signed_in? %>
<% if !current_user.try(:profile) %>
Signed in as<div><%= link_to current_user.email, new_user_profile_path(current_user.id) %></div><br /><br />
<% else %>
Signed in as<div><%= link_to current_user.email, edit_user_profile_path(current_user.id) %></div><br /><br />
<% end %>
Not you? <%= link_to "Sign out", logout_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "Sign in", new_user_session_path %>
<% end %>
路线:
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format)