2012-02-11 12 views
0

レール3のバリデーションに問題があります。モデルフォームのフィールドの1つを検証しようとしています。つまり、空白にしないでください。しかし、問題はまだ私はそのテキストフィールドを埋めるにもかかわらず、それはエラーを示しています。起こりうる問題は何か? モデルファイル:rails 3バリデーションでフォーム提出を許可しない

class Page < ActiveRecord::Base 

validates :title, :presence => true 
end 

_form.html.erb

<%= form_for(@page) do |f| %> 
    <% if @page.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@page.errors.count, "error") %> prohibited this post from being saved:</h2> 

     <ul> 
     <% @page.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.label :body %><br /> 
    <%= f.text_area :body %> 
    </p> 
    <p> 
    <%= f.label :author %><br /> 
    <%= f.text_field :author %> 
    </p> 
    <p> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </p> 
    <p> 
    <%= f.label :reference %><br /> 
    <%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %> 
    </p> 
    <%= f.submit "Submit" %> 
<% end %> 

そして、ここでは私のコントローラファイルです:

class PagesController < ApplicationController 

    def index 
    @total = Page.count 
    @pages = Page.find(:all) 
    end 

    def new 
    @page = Page.new 


    end 

    def create 
    @page = Page.new(params[:pages]) 
    if @page.save 
     redirect_to pages_path, :notice => "The data has been saved!" 
    else 
     render "new" 
    end 
    end 

    def edit 
    @page = Page.find(params[:id]) 

    if request.post? 
     @page.title = params[:title] 
     @page.author = params[:author] 
     @page.email = params[:email] 
     @page.body = params[:body] 
     @page.reference = params[:reference] 
     @page.save 
     redirect_to :action => 'index' 
    end 
    end 

    def destroy 
    @page = Page.find(params[:id]) 
    @page.destroy 
    redirect_to :action => 'index' 
    end 
end 

次のように私のroutes.rbをです:

Rorapp::Application.routes.draw do 
    get "home/index" 
    post "home/search" 
    get "home/_help" 
    get "home/create" 
    get "pages/index" 
    get "pages/new" 
    post "pages/new" 
    post "pages/edit" 
    get "pages/edit" 
resources :pages 
    # 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 => 'home#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 
+0

エラーを投稿できますか? –

+0

@AlexMarchant mate! theresエラーなし! – Maverick

答えて

1

あなたの共同の作成メソッドコントローラはparam[:pages]を指します。これはparam[:page]でしょうか?

+0

実際にそれを自分で整理しました。あなたが言及した最初の点は正しいです。おかげで多く – Maverick

+0

確かに - 2番目の点は実際に間違っています。 –

+0

混乱を避けるために削除します。 –

2

ルートが重複しています。

は、以下にあなたのroutes.rbをファイルを交換してみてください:

routes.rbを

Rorapp::Application.routes.draw do 
    get "home/index" 
    post "home/search" 
    get "home/_help" 
    get "home/create" 
    resources :pages 
    root :to => 'home#index' 
    match ':controller(/:action(/:id(.:format)))' 
end 

また、あなたがそうあなたのCRUDが完全になる、あなたのPagesControllerupdateメソッドを作成することができます。

関連する問題