2016-07-27 61 views
0

我有一個simple_form,帶有一個分組集合select和兩個輸入字段。我對這兩個字段都有要求:true,但它仍然允許通過空輸入。字段名稱旁邊會出現一個「必需」星號,但就是這樣。有什麼辦法可以防止空格輸入通過表單?Rails'required:true'無法完全工作

new.rb

<h1>New Article</h1> 

<%= render 'form', article: @article %> 

<%= link_to 'Back', articles_path(category_id: params[:category_id]) %> 

_form.rb

<%= simple_form_for(article, html: {class: 'form-vertical'}) do |f| %> 
    <% if article.errors.any? %> 
    <div id="error_explanation"> 
     <h4><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h4> 

     <ul> 
     <% article.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 

    <%# field being selected, parent collection, subcollection, displayed, key, value %> 
    <%= f.grouped_collection_select :subcategory_id, Category.all,:subcategories,:name, :id,:name, {required: true} %> 
    <%= f.input :title, required: true %> 
    <%= f.input :content, input_html: { rows: 20 }, required: true%> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

articles_controller.rb

class ArticlesController < ApplicationController 
    before_action :set_article, only: [:show, :edit, :update, :destroy] 

    # GET /articles 
    # GET /articles.json 
    def index 
    if params[:category_id].blank? && params[:subcategory_id].blank? 
     @articles = Article.all.order("created_at DESC") 
    elsif params[:subcategory_id].blank? 
     @articles = Article.where(category_id: params[:category_id]) 
    else 
     @articles = Article.where(subcategory_id: params[:subcategory_id]).order("created_at DESC") 
    end 
    end 

    # GET /articles/1 
    # GET /articles/1.json 
    def show 
    end 

    # GET /articles/new 
    def new 
    @article = Article.new 
    end 

    # GET /articles/1/edit 
    def edit 
    end 

    # POST /articles 
    # POST /articles.json 
    def create 
    @parameters = article_params 
    @parameters[:category_id] = Subcategory.find(@parameters[:subcategory_id]).category_id 
    @article = Article.new(@parameters) 

    respond_to do |format| 
     if @article.save 
     format.html { redirect_to @article, notice: 'Article was successfully created.' } 
     format.json { render :show, status: :created, location: @article } 
     else 
     format.html { render :new } 
     format.json { render json: @article.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /articles/1 
    # PATCH/PUT /articles/1.json 
    def update 
    respond_to do |format| 
     if @article.update(article_params) 
     format.html { redirect_to @article, notice: 'Article was successfully updated.' } 
     format.json { render :show, status: :ok, location: @article } 
     else 
     format.html { render :edit } 
     format.json { render json: @article.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /articles/1 
    # DELETE /articles/1.json 
    def destroy 
    @article.destroy 
    respond_to do |format| 
     format.html { redirect_to root_path, notice: 'Article was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_article 
     @article = Article.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def article_params 
     params.require(:article).permit(:title,:content,:subcategory_id) 
    end 
end 
+0

只需添加'validates_presence_of:something'你'Article'模型(標題,內容等) – marmeladze

回答

0

您需要使用驗證,該邏輯模型內部完成。 請確保您閱讀並遵守ActiveRecord Validations Guide,因爲它會爲您節省大量時間。 實施例從導採取:

class Person < ApplicationRecord 
    validates :name, presence: true 
end 



Person.create(name: "John Doe").valid? # => true 
Person.create(name: nil).valid? # => false 

基本上當所述對象被保存,它運行驗證和如果它不是有效它將填補與消息變量的誤差。

+0

對,這就是我沒有做的。謝謝! – Alex

0

簡單的表格文檔狀態...

默認情況下是必需的所有輸入。當表單對象具有連接到其字段的驗證 時,簡單表單會告知必需字段和 可選字段。出於性能方面的原因,此檢測爲 跳過使用條件選項的驗證,如 :if和:unless。

這意味着,所有你需要做的就是添加到您的模型/ article.rb文件

class Article < ActiveRecord::Base 

    validates :title, presence: true 
    validates :content, presence: true