在.build方法中设置默认值

时间:2015-05-18 20:31:45

标签: ruby-on-rails

我在创建学校时尝试提交嵌套表单,您也可以使用它创建管理员。当我尝试使用默认值owner设置为true并将状态设置为活动来保存表单时,它会保存表单但不会将这些值保存在数据库中

class SchoolsController < ApplicationController
    before_filter :authenticate_admin!, except: [:index, :new, :create]
    def show
        @school = School.friendly.find(params[:id])
    end
    def new
        @school = School.new
        @admin = @school.admins.build(:owner => true, :status => "active")
    end
    def create
        @school = School.new(school_params)
        if @school.save
            redirect_to new_admin_session_path
        else
            render 'new'
        end

    end
    def edit
        @school = School.find_by_slug(params[:id])
    end
    def update
        @school = School.find_by_slug(params[:id])

        if @school.update_attributes(school_params)
            redirect_to :action => 'show', :id => @school
        else
         render :action => 'edit'
        end
    end
    def team
        @teams = Admin.all
    end
    private

    def school_params
        params.require(:school).permit(:school_name, :latitude, :longitude, :radius, admins_attributes: [ :first_name, :last_name, :email, :password, :password_confirmation, :image ])
    end
end
class School < ActiveRecord::Base
    has_many :admins
    accepts_nested_attributes_for :admins

    extend FriendlyId
    friendly_id :school_name, use: :slugged

    def should_generate_new_friendly_id?
        school_name?
    end

    # Validation

    validates :school_name, presence: true, uniqueness: true
    validates :latitude, presence: true
    validates :longitude, presence: true
    validates :radius, presence: true, numericality: { only_integer: true }

end
%nav.navbar.navbar-default.navbar-fixed-top{role:"navigation"}
    %div.container
        %div.navbar-header
            %button.navbar-toggle.collapsed{"data-toggle" => "collapse", "data-target" => "#navbar", "aria-expanded" => "false", "aria-controls" => "navbar"}
                %span.sr-only Toggle Navigation
                %span.icon-bar
                %span.icon-bar
                %span.icon-bar
            %a.navbar-brand{"href" => "/", "id"=> "brand"} QuickAlert
        %div#navbar.collapse.navbar-collapse
            %ul.nav.navbar-nav.main-menu
                %li.active
                    %a{"href" => "#"} Home
                %li
                    %a{"href" => "#about"} About
                %li
                    %a{"href" => "#contact"} Contact
%div.container-fluid
    %div.row
        %div.school-create
            - if @school.errors.any?
                %ul
                    - @school.errors.full_messages.each do |msg|
                        %li
                            = msg
            %div#map-check
            %h2.header School Info
            = form_for @school do |f|
                = f.label :school_name, "School Name"
                = f.text_field :school_name, :class => 'form-control'
                = f.label :latitude, "Latitude"
                = f.text_field :latitude, :class => 'form-control', :id => "latitude"
                = f.label :longitude, "Longitude"
                = f.text_field :longitude, :class => 'form-control', :id => "longitude"
                = f.label :radius, "Radius"
                = f.text_field :radius, :class => 'form-control', :id => "radius"
                %div.admin-fields
                    %h2.header Admin Info
                    = f.fields_for :admins do |ff|
                        = ff.label :first_name
                        = ff.text_field :first_name, :class => 'form-control'
                        = ff.label :last_name
                        = ff.text_field :last_name, :class => 'form-control'
                        = ff.label :email
                        = ff.text_field :email, :class => 'form-control'
                        = ff.label :password
                        = ff.password_field :password, :class => 'form-control'
                        = ff.label :password_confirmation
                        = ff.password_field :password_confirmation, :class => 'form-control'
                        = ff.file_field :image
                = f.submit :class => 'submit-button btn btn-primary'

0 个答案:

没有答案