简体   繁体   中英

No route matches [POST] “/users/1/contacts/new”

app/views/contacts/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 %>

My routes when I run 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

config/routes.rb

Rails.application.routes.draw do
      devise_for :users

      resources :users do
        resources :contacts
      end
    end

My new and create action in 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

This is the issue: I get the error: No route matches [POST] "/users/1/contacts/new" when I submit the form at http://localhost:3000/users/1/contacts/new . What could be the issue?

This:

 <%= simple_form_for @contact, :url => new_user_contact_path do |f| %>

should be:

 <%= simple_form_for @contact, :url => user_contacts_path do |f| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM