2012-12-27 71 views
1

我經歷了railstutorial.org書和得到錯誤NoMethodError在UsersController#創建

時,我立即登記我得到的形狀誤差

NoMethodError在UsersController#創建

我user_controller類是

class UsersController < ApplicationController 


    def new 
     @user = User.new 
    end 

    def show 
     @user = User.find(params[:id]) 
    end 

    def create 
     @user = User.new(params[:user]) 
     if @user.save 
      redirect to @user 
     else 
     render 'new' 
     end 
    end 

end 

的routes.rb文件

SampleApp::Application.routes.draw do 
    resources :users 


    root to: 'static_pages#home' 


match '/signup', to: 'users#new' 

match '/help' , to:'static_pages#help' 
match '/about' ,to:'static_pages#about' 
match '/contact' , to: 'static_pages#contact' 


    # The priority is based upon order of creation: 
    # first created -> highest priority. 

    # Sample of regular route: 
    # match 'products/:id' => 'catalog#view' 
    # Keep in mind you can assign values other than :controller and :action 

    # Sample of named route: 
    # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase 
    # This route can be invoked with purchase_url(:id => product.id) 

    # Sample resource route (maps HTTP verbs to controller actions automatically): 
    # resources :products 

    # Sample resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

    # Sample resource route with sub-resources: 
    # resources :products do 
    #  resources :comments, :sales 
    #  resource :seller 
    # end 

    # Sample resource route with more complex sub-resources 
    # resources :products do 
    #  resources :comments 
    #  resources :sales do 
    #  get 'recent', :on => :collection 
    #  end 
    # end 

    # Sample resource route within a namespace: 
    # namespace :admin do 
    #  # Directs /admin/products/* to Admin::ProductsController 
    #  # (app/controllers/admin/products_controller.rb) 
    #  resources :products 
    # end 

    # You can have the root of your site routed with "root" 
    # just remember to delete public/index.html. 
    # root :to => 'welcome#index' 

    # See how all your routes lay out with "rake routes" 

    # This is a legacy wild controller route that's not recommended for RESTful applications. 
    # Note: This route will make all actions in every controller accessible via GET requests. 
    # match ':controller(/:action(/:id))(.:format)' 
end 

任何人都可以請幫助爲什麼我得到這個錯誤。我只是一個初學者在rails.so不能糾正這個錯誤。

+3

不知道它是否是一個錯字,但'重定向到@ user'的行實際上是'redirect_to @ user'。 – MurifoX

+0

它的工作現在! –

+0

我會將其作爲回答發佈,以便您可以接受它嗎? – MurifoX

回答

2

它實際上是控制器上創建操作的重定向部分中的拼寫錯誤。將redirect to @user更改爲redirect_to @user解決了它。

相關問題