应用程序/视图/联系人/ new.html.erb
<h2>Add New Contact</h2>
<div class="row">
<div class="col-md-8">
<%= simple_form_for @contact, :url => new_user_contact_path do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :date_of_birth, as: :date, discard_year:true, order:[:day, :month] %>
</div>
<div class="form-group">
<%= f.label :sex %>
<%= f.select :sex, [:male, :female], class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_area :address, class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit, "Submit", class: "btn btn-success btn-md btn-block" %>
</div>
</div>
<% end %>
</div>
</div>
<%= link_to "Back", user_contacts_path %>
我运行rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_contacts GET /users/:user_id/contacts(.:format) contacts#index
POST /users/:user_id/contacts(.:format) contacts#create
new_user_contact GET /users/:user_id/contacts/new(.:format) contacts#new
edit_user_contact GET /users/:user_id/contacts/:id/edit(.:format) contacts#edit
user_contact GET /users/:user_id/contacts/:id(.:format) contacts#show
PATCH /users/:user_id/contacts/:id(.:format) contacts#update
PUT /users/:user_id/contacts/:id(.:format) contacts#update
DELETE /users/:user_id/contacts/:id(.:format) contacts#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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
index GET /index(.:format) redirect(301, /)
root GET / high_voltage/pages#show {:id=>"index"}
page GET /pages/*id high_voltage/pages#show
配置/ routes.rb中
Rails.application.routes.draw do
devise_for :users
resources :users do
resources :contacts
end
end
我在new
create
和ContactsController
行动
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
@contact.user = current_user
if @contact.save
flash[:success] = "Contact was successfully created!"
redirect_to @contact.user
else
render :new
end
end
这是问题所在:
当我在http://localhost:3000/users/1/contacts/new提交表单时收到错误:No route matches [POST] "/users/1/contacts/new"
。可能是什么问题?
答案 0 :(得分:3)
此:
<%= simple_form_for @contact, :url => new_user_contact_path do |f| %>
应该是:
<%= simple_form_for @contact, :url => user_contacts_path do |f| %>