2

提交“创建我的个人资料”后出现错误

路由错误

没有路线匹配 [POST] "/users/3/profiles"

其次,我重新启动了 webbrick cli = "rails s",并且在输入“http://localhost:3000/users/1/profile/new”后收到错误消息“未定义的方法 `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
4

1 回答 1

7

据我了解,rails 无法知道form_for ([@user, @profile])(假设@profile是一条新记录)是否要使用动词 POST 路由到/users/:user_id/profileor /users/:user_id/profiles。一般来说,它会采用复数形式,所以如果你想要单数形式,你必须通过写作明确告诉它

form_for @profile, url: user_profile_path(@user)
于 2012-07-05T02:06:11.343 回答