рельсы 3 проверки, не позволяющие отправить форму - PullRequest
0 голосов
/ 11 февраля 2012

У меня проблема с проверками в рельсах 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

Мой route.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 ]

2 голосов
/ 11 февраля 2012

У вас есть дубликаты маршрутов.

Попробуйте заменить ваш файл rout.rb следующим:

rout.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 голос
/ 11 февраля 2012

Метод создания в вашем контроллере ссылается на param[:pages]. Это должно быть param[:page]?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...