在form_for上具有命名错误的Rails导致未知路径错误

时间:2016-10-03 17:58:07

标签: ruby-on-rails ruby ruby-on-rails-4

问题是我得到了这个令人讨厌的小控制器错误...... 联系人#new中的NoMethodError 未定义的方法`contacts_path'对于#<#:0x0000000124b228>  你的意思是? contact_path

在我看来:联系#new

<%= form_for @contact do |f| %>

<div class="col-xs-6 form-group contact-input">
<h1><%= f.label :Feedback %></h1>
<%= f.text_area :text, class: "input-lg form-control", rows: "10" %>

<%= f.submit "Send Feedback", class: "btn btn-primary btn-lg" %>
</div>

<% end %>

controller:contacts_controller.rb

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new

    if @contact.save
      redirect_to '/'
      flash[:success] = "Thanks for the Post!"
    else
      redirect_to contact_path
      flash[:alert] = "Please provide input!"
    end
  end
end

迁移:

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts do |t|

      t.text :text

      t.timestamps null: false
    end
  end
end

错误是抱怨视图中的第3行。我的模型是contact.rb incase我有一个复数问题,但我真的不认为我这样做。任何帮助将非常感激。这是轨道4.2 ......我也是铁轨初学者。

我知道我还没有设置闪光灯。

rake routes:

Prefix Verb   URI Pattern                                   Controller#Action
              root GET    /                                             static_pages#home
             about GET    /about(.:format)                              static_pages#about
              news GET    /news(.:format)                               static_pages#news
         advertise GET    /advertise(.:format)                          static_pages#advertise
           fishing GET    /fishing(.:format)                            static_pages#fishing
            signup GET    /signup(.:format)                             users#new
             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
           contact GET    /contact(.:format)                            contacts#new
     contact_index GET    /contact(.:format)                            contact#index
                   POST   /contact(.:format)                            contact#create
       new_contact GET    /contact/new(.:format)                        contact#new
      edit_contact GET    /contact/:id/edit(.:format)                   contact#edit
                   GET    /contact/:id(.:format)                        contact#show
                   PATCH  /contact/:id(.:format)                        contact#update
                   PUT    /contact/:id(.:format)                        contact#update
                   DELETE /contact/:id(.:format)                        contact#destroy
    forum_comments GET    /forums/:forum_id/comments(.:format)          comments#index
                   POST   /forums/:forum_id/comments(.:format)          comments#create
 new_forum_comment GET    /forums/:forum_id/comments/new(.:format)      comments#new
edit_forum_comment GET    /forums/:forum_id/comments/:id/edit(.:format) comments#edit
     forum_comment GET    /forums/:forum_id/comments/:id(.:format)      comments#show
                   PATCH  /forums/:forum_id/comments/:id(.:format)      comments#update
                   PUT    /forums/:forum_id/comments/:id(.:format)      comments#update
                   DELETE /forums/:forum_id/comments/:id(.:format)      comments#destroy
            forums GET    /forums(.:format)                             forums#index
                   POST   /forums(.:format)                             forums#create
         new_forum GET    /forums/new(.:format)                         forums#new
        edit_forum GET    /forums/:id/edit(.:format)                    forums#edit
             forum GET    /forums/:id(.:format)                         forums#show
                   PATCH  /forums/:id(.:format)                         forums#update
                   PUT    /forums/:id(.:format)                         forums#update
                   DELETE /forums/:id(.:format)                         forums#destroy
            logout DELETE /logout(.:format)                             sessions#destroy
          sessions POST   /sessions(.:format)                           sessions#create
       new_session GET    /sessions/new(.:format)                       sessions#new

routes.rb:

Rails.application.routes.draw do
  root 'static_pages#home'
  get 'about' =>      'static_pages#about'
  get 'news' =>       'static_pages#news'
  get 'advertise' =>  'static_pages#advertise'
  get 'fishing' =>    'static_pages#fishing'

  get 'signup' =>     'users#new'
  resources :users

  get 'contact' =>    'contacts#new'
  resources :contact

  resources :forums do
    resources :comments
  end

  delete 'logout' =>     'sessions#destroy'
  resources :sessions, only: [:new, :create] 
end

2 个答案:

答案 0 :(得分:0)

resources :contact

中的resources :contacts更改为routes.rb

答案 1 :(得分:0)

我是个白痴,感谢Zepplock只是想着我的路线显示了我的问题。得到   得到'联系'=&gt; “接触#新” 是我的问题。我也切换了我的资源:联系人:联系人。我所有的问题都解决了。谢谢你再次要求正确的东西Zepplock!