没有任何错误被打印出来

时间:2014-07-11 18:11:18

标签: ruby-on-rails-4

我在页面中有两个表格,如下所示,甚至包括那些未打印的错误打印部分

<code>
<div id="tabs-2">
    <% if @user.personal %>
    <h2>Personal Details</h2>
    <div class="image"><%= image_tag (@user.personal.avatar_url(:thumb))if @user.personal.avatar? %></div>
    <b>First Name</b>:  <%= @user.personal.first_name %><br>
    <b>Middle Name</b>:  <%= @user.personal.middle_name %><br>
    <b>Last Name</b>:  <%= @user.personal.last_name %><br>
    <b>Date of Birth</b> <%= @user.personal.date_of_birth %><br>
    <b>Gender</b>:  <%= @user.personal.gender %><br>
    <b>Category</b> <%= Category.find(@user.personal.category_id).category %><br>
    <b>Blood Group</b>:  <%= @user.personal.blood_group %><br>
    <b>Fathers_name</b>:  <%= @user.personal.fathers_name %><br>
    <b>Mothers_name</b>:  <%= @user.personal.mothers_name %><br>
    <% else %>
      <%= form_for([@user, Personal.new]) do |f| %>
        <% if @user.errors.any? %>
          <div id="error_explanation">
            <h2><%= pluralize(@user.errors.count, "error") %> prohibited this candidate from being saved:</h2>
            <ul>
            <% @user.personal.errors.full_messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
            </ul>
          </div>
        <% end %>
         <p>
              <%= f.label :first_name  %><br/>
              <%= f.text_field :first_name %>
      </p>
        <p>
              <%= f.label :middle_name  %><br/>
              <%= f.text_field :middle_name %>
      </p>
        <p>
              <%= f.label :last_name  %><br/>
              <%= f.text_field :last_name %>
      </p>
      <p>
              <%= f.label :date_of_birth, "Date of Birth" %><br/>
              <%= f.date_select :date_of_birth, start_year: 1985, end_year: 1999  %>
      </p>
      <p>
              <%= f.label :gender %><br/>
              <%= f.select :gender, options_for_select(%w[M F O]) %>
      </p>
      <p>
              <%= f.label :blood_group %><br/>
              <%= f.select :blood_group, options_for_select(%w[AB A+ B+ O ]) %>
      </p>
      <p>
              <%= f.label :fathers_name %><br/>
              <%= f.text_field :fathers_name %>
      </p>
      <p>
              <%= f.label :mothers_name  %><br/>
              <%= f.text_field :mothers_name %>
      </p>
      <p>
              <%= f.label :category_id %></br>
              <%= select("personal", "category_id", Category.all.collect {|c| [c.category, c.id]}) %>
      </p>

      <p>
              <%= f.label :avatar %>
              <%= f.file_field :avatar %>
      </p>
      <p><%= f.submit "Submit", data: {confirm: "Are you sure information once submitted cant be modified ?"} %></p>
      <% end %>
  <% end %>
</code>

这是第一个进入

的表格
app/views/user_dashboard/home.html.erb

我无法弄清楚为什么在验证失败时不会打印错误 出现在personal.rb

class Personal < ActiveRecord::Base
  # every personal information belongs to a particular user
  belongs_to :user
  # carrierwave uploader
  mount_uploader :avatar, AvatarUploader
  # validations
  validates :first_name, presence: true, length: { maximum: 24}
  validates :middle_name, length: { maximum: 24}
  validates :last_name, presence: true, length: { maximum: 24}
  validates :date_of_birth, presence: true
  validates :gender, presence: true, length: { maximum: 1}
  validates :category_id, presence: true
  validates :blood_group, presence: true, length: { maximum: 2}
  validates :fathers_name, presence: true, length: { maximum: 24}
  validates :mothers_name, presence: true, length: { maximum: 24}
  validates :avatar, presence: true
  end

这是personals_controller.rb

class PersonalsController < ApplicationController
  before_action :set_personal, only: [:show, :edit, :update, :destroy]

  # This helps to create personals
  def create
   @user = current_user
   @personal = @user.create_personal(personal_params)
   redirect_to home_path
  end


  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def personal_params
      params.require(:personal).permit(:user_id, :category_id, :avatar, :date_of_birth, :gender, :blood_group, :fathers_name, :mothers_name, :address_present, :first_name, :last_name, :middle_name)
    end
end

这里有一些日志

Started POST "/users/36/personals" for 127.0.0.1 at 2014-07-11 23:57:20 +0530
Started POST "/users/36/personals" for 127.0.0.1 at 2014-07-11 23:57:20 +0530
Processing by PersonalsController#create as HTML
Processing by PersonalsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8qku1i/aIlz1aBdypnARzpiKuChWupPCMesjGoNmaTY=", "personal"=>{"first_name"=>"", "middle_name"=>"", "last_name"=>"", "date_of_birth(1i)"=>"1985", "date_of_birth(2i)"=>"7", "date_of_birth(3i)"=>"11", "gender"=>"M", "blood_group"=>"AB", "fathers_name"=>"", "mothers_name"=>"", "category_id"=>"1"}, "commit"=>"Submit", "user_id"=>"36"}
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8qku1i/aIlz1aBdypnARzpiKuChWupPCMesjGoNmaTY=", "personal"=>{"first_name"=>"", "middle_name"=>"", "last_name"=>"", "date_of_birth(1i)"=>"1985", "date_of_birth(2i)"=>"7", "date_of_birth(3i)"=>"11", "gender"=>"M", "blood_group"=>"AB", "fathers_name"=>"", "mothers_name"=>"", "category_id"=>"1"}, "commit"=>"Submit", "user_id"=>"36"}
  User Load (0.1ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 36  ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.1ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 36  ORDER BY `users`.`id` ASC LIMIT 1
   (0.1ms)  BEGIN
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
   (0.1ms)  ROLLBACK
  Personal Load (0.1ms)  SELECT  `personals`.* FROM `personals`  WHERE `personals`.`user_id` = 36 LIMIT 1
  Personal Load (0.1ms)  SELECT  `personals`.* FROM `personals`  WHERE `personals`.`user_id` = 36 LIMIT 1
Redirected to http://localhost:3000/home
Redirected to http://localhost:3000/home
Completed 302 Found in 15ms (ActiveRecord: 0.4ms)
Completed 302 Found in 15ms (ActiveRecord: 0.4ms)




Started GET "/home" for 127.0.0.1 at 2014-07-11 23:57:20 +0530
Started GET "/home" for 127.0.0.1 at 2014-07-11 23:57:20 +0530
Processing by UserDashboardController#home as HTML
Processing by UserDashboardController#home as HTML

的routes.rb

# http://www.quora.com/How-can-I-use-nested-routes-with-Devise
  devise_for :users, :controllers => {:registrations => "registrations"}, :path => 'accounts'
  # users has personal academics and applications hence nested routes
  resources :users do
    resources :personals
    resources :academics
    resources :applications
  end

  # user_dashboard controller
  post 'user_dashboard/personal_creator'
  # Controlpanel route
  get 'controlpanels/controlpanel'
  get 'controlpanels/resetranks'
  get 'controlpanels/generateranks'
  # verify routes
  get 'vusers/verify'
  # dashboard url shortening
  match '/home',  to: 'user_dashboard#home', via: 'get'
  match '/controlpanel',  to: 'controlpanels#controlpanel', via: 'get'
  match '/verify', to: 'vusers#verify', via: 'get'
  # routes for streams
  resources :streams
  # routes for categories
  resources :categories
  # routes for cutoffs
  resources :cutoffs

2 个答案:

答案 0 :(得分:0)

有一种更清洁的方法可以做到这一点。在你的控制器方法中(假设它在家)你可以这样做:

def home
  @user = User.find(params[:id]) #your logic to find user
  @personal = @user.build_personal #assuming you have user has_one personal association
end

然后以您的形式

<%= form_for([@user, @personal]) do |f| %>
    <% if @personal.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@personal.errors.count, "error") %> prohibited this candidate from being saved:</h2>
        <ul>
          <% @personal.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>
<% end %>

<强>更新

在您的创建操作中(假设此表单需要创建操作),您需要具有以下内容:

def create
  @user = User.find(params[:id]) # logic to find user
  @personal = @user.build_personal(personal_params)
  respond_to do |format|
    if @personal.save
      format.html { redirect_to @personal }
    else
      format.html { render :action => "home" }
    end
  end
end

private
def personal_params
  params.require(:personal).permit(:your_attributes)
end

<强>更新

如果您的表单通过了验证,则create_personal将只创建您的记录,因为它不会直接转到下面的步骤,即重定向到home_path,因此没有错误

您甚至可以在日志中看到它

User Load (0.1ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 36  ORDER BY     `users`.`id` ASC LIMIT 1
(0.1ms)  BEGIN
(0.1ms)  BEGIN
(0.1ms)  ROLLBACK
(0.1ms)  ROLLBACK
Personal Load (0.1ms)  SELECT  `personals`.* FROM `personals`  WHERE  `personals`.`user_id` = 36 LIMIT 1
Personal Load (0.1ms)  SELECT  `personals`.* FROM `personals`  WHERE `personals`.`user_id` = 36 LIMIT 1
Redirected to http://localhost:3000/home

答案 1 :(得分:0)

class PersonalsController < ApplicationController

def create
     @user = current_user
     @personal = @user.build_personal(personal_params)
     if @personal.save
       flash[:notice] = "success"
     else
       flash[:alert] = @personal.errors.full_messages.each{|e|}
     end
     redirect_to home_path

  end

end

这是答案吗?