Simple_form required field does not work - Ruby on Rails

纵饮孤独 提交于 2019-12-14 03:45:32

问题


I have a submiting form in a RoR app, built with simple_form. When the fields are blank, the app still goes to the next step without prompting an error or warning. The fields are supposed to be by default required: true; but even writing it manually does not work.

The app has 3 steps: NewPost (new view) -> Preview (create view) -> Post.

It would be more clear with a extract of my controller and views:

def new
    @post= Post.new
end

def create
    @post = Post.new(params.require(:post).permit(:title, :category_id))

    if params[:previewButt] == "Continue to Preview your Post"
      render :create
    elsif params[:createButt] == "OK! Continue to Post it"
      if @post.save!
      redirect_to root_path
      else
      render :new
      end 
    elsif params[:backButt] == "Make changes"
      render :new
    end
  end

My New view (extract):

<%= simple_form_for @post do |form| %>
<%= form.input :title, input_html: { class: 'post-title-input' }, label: false, hint: '"Your post title"' %>
<%= form.collection_radio_buttons(:category_id, Category.all, :id, :name, :item_wrapper_class => "selectable" ) %>
<%= form.button :submit , name: "previewButt", value: "Continue to Preview your Post",  class: "btn btn-lg btn-primary btn-preview" %>
<% end %>

My Create view (extract):

<%= simple_form_for @post do |form| %>
<%= form.hidden_field :title, {:value => @post.title} %>
<%= form.hidden_field :category_id, {:value => @post.category_id} %>
<% end %>

Note that the problem is not when saving, the model definitions work ok, the problem is only in the simple_form.

class Post < ActiveRecord::Base
    belongs_to :category
    validates :title, presence: true
    validates :category_id, presence: true
end

SOLUTION thanks to the DickieBoy hint:

Change the controller to:

      def create
        @post = Post.new(params.require(:post).permit(:title, :category_id))

        if params[:previewButt] == "Continue to Preview your Post"
           if @post.valid? 
              render :create
           else
              render :new 
        elsif params[:createButt] == "OK! Continue to Post it"
          if @post.save!
          redirect_to root_path
          else
          render :new
          end 
        elsif params[:backButt] == "Make changes"
          render :new
        end
      end

回答1:


Its going to the create view because you don't tell it any different.

@post = Post.new(params.require(:post).permit(:title, :category_id))

Creates a new instance of Post with the parameters given from the form which are empty. The new call does nothing in terms of validation. You want something like:

@post = Post.new(params.require(:post).permit(:title, :category_id))

if @post.valid? && params[:previewButt] == "Continue to Preview your Post"
....



回答2:


There is a setting in simple_form initialiser which activates client side validation. Irrespective client side validation, you have to keep the server side validation as well (what you are doing currently). To implement the client side validation using simple_form, do the following:

config/initializers/simple_form.rb

config.browser_validations = true


来源:https://stackoverflow.com/questions/29151440/simple-form-required-field-does-not-work-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!