2012-02-11 2 views
0

레일 3의 유효성 검사에 문제가 있습니다. 모델 시트의 필드 중 하나의 유효성을 검사하려고하는 모델이 있습니다. 즉, 비어 있으면 안됩니다. 하지만 문제는 내가 텍스트 필드를 채웠지 만 여전히 오류를 보여줍니다. 가능한 문제점은 무엇입니까? 모델 파일은 다음과 같습니다레일 3 폼 검증을 허용하지 않는 유효성 검사

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은 다음과 같습니다 :

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 %> 

그리고 여기 내 컨트롤러 파일입니다

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 no error! – Maverick

답변

1

귀하의 공동 작업에서 create 메소드 컨트롤러는 param[:pages]을 나타냅니다. 이게 param[:page]일까요?

+0

실제로 직접 수정했습니다. 언급 한 첫 번째 사항은 옳습니다. 덕분에 많이 – Maverick

+0

사실 - 두 번째 요점은 실제로 잘못되었습니다. –

+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 

은 또한 당신이 당신의 PagesControllerupdate 방법을 만들 수 있습니다, 당신의 CRUD가 완료됩니다 있도록.