简体   繁体   English

如何使用ROR在数据库中保存表单值

[英]How to save form values in database using ROR

Can anybody help me to save these form values in database.I have already created one form.When I clicked on submit button the below validation message is coming. 有人可以帮我将这些表单值保存在数据库中吗?我已经创建了一个表单。当我单击“提交”按钮时,下面的验证消息到来了。

1 error prohibited this post from being saved:

Gender must be accepted  

Please check the code and help me to save all values in DB.If any error found please make it correct. 请检查代码并帮助我将所有值保存在DB中。如果发现任何错误,请使其正确。

My code is as follows: 我的代码如下:

views/students/index.html.erb 意见/学生/ index.html.erb

 <h1>Choose the option</h1>
    <p>
      <%= link_to "Enter student data",students_new_path %>
    </p>
    <p>
      <%= link_to "Display your data",students_show_path %>
    </p>

views/students/new.html.erb

<h1>Enter your data</h1>
<%= form_for @student,:url => {:action => 'create'} do |f| %>
    <% if @student.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@student.errors.count, "error") %> prohibited this post from being saved:</h2>

          <ul>
            <% @student.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
    <p>
      <label for="name">Name:</label>
      <%= f.text_field :name,placeholder:"Enter your name" %>
    </p>
    <p>
      <label for="gender">Gender:</label><br>
      <%= f.radio_button :gender,'Male',:checked => true %>
      <%= f.label :Male  %>
      <%= f.radio_button :gender,'Female' %>
      <%= f.label :Female %>
    </p>
    <p>
      <label for="city">Select the City</label>
      <%= f.select(:city,options_for_select([['Bhubaneswar','Bhubaneswar'],['Cuttack','Cuttack'],['Behrempur','Behrempur'],['Puri','Puri']],selected: "Puri")) %>
    </p>
    <p>
      <%= f.check_box :terms_service  %> accept terms and service
    </p>
    <p>
      <%= f.submit "Submit" %>
    </p>
<% end %>

controller/students_controller.rb 控制器/ students_controller.rb

class StudentsController < ApplicationController
  def index

  end
  def show

  end
  def new
    @student=Student.new

  end
  def create
  @student=Student.new(users_params)
    if @student.save
      flash[:notice]="You have signed up successfully.."
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:notice]="You have not signed up successfully"
      flash[:color]="invalid"
      render :new
    end
  end
  private
  def users_params
    params.require(:student).permit(:name,:gender,:city,:terms_service)
  end
end

model/student.rb 模型/ student.rb

class Student < ActiveRecord::Base
  validates :name ,:presence => true,:length => { :minimum => 6 }
  validates :gender, :acceptance => true
  validates :terms_service, :acceptance => true
end

migrate\\20150114061737_create_students.rb 迁移\\ 20150114061737_create_students.rb

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.string :name
      t.string :gender
      t.string :city
      t.string :terms_service

      t.timestamps null: false
    end
  end
end

Please help me to run this code successfully.Thanks in advance.. 请帮助我成功运行此代码。谢谢。

Validation type used for gender field is wrong ie acceptance validation is used when you want to check if a check-box is checked by user in the form. 用于性别字段的验证类型错误,即,当您要检查用户是否在表单中选中复选框时,将使用接受验证。 In your situation you should use 在您的情况下,您应该使用

validates :gender, :presence => true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM