rails 3验证不允许表单提交

时间:2012-02-11 00:05:04

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-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

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

您也可以在update中创建PagesController方法,这样您的CRUD就会完整。

答案 1 :(得分:1)

控制器中的create方法引用param[:pages]。这应该是param[:page]吗?