提交“创建我的个人资料”
后发生错误路由错误
没有路线匹配[POST]“/ users / 3 / profiles”
其次,我重新启动了webbrick cli =“rails s”,输入“http:// localhost:3000 / users / 1 / profile / new”后,我收到错误消息“undefined method`user_profiles_path'”。显然,我使用了“resource:profile”非嵌套资源。我正在撕扯我的头发,这些错误发生了什么?
<h1>About You</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for ([@user, @profile]) do |f| %>
<%= f.label :name, "First name:" %>
<%= f.text_field :name %>
<%= f.label :surname, "Surname:" %>
<%= f.text_field :surname %>
<%= f.submit "Create my profile", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
resources :users do
resource :profile
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) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / static_pages#home
答案 0 :(得分:7)
据我了解,rails无法知道form_for ([@user, @profile])
(假设@profile
是新记录)是要路由到/users/:user_id/profile
还是/users/:user_id/profiles
,用动词POST。一般来说,它将采用复数形式,因此您必须通过编写明确告诉它是否需要单数形式
form_for @profile, url: user_profile_path(@user)