Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple_form required field does not work - Ruby on Rails

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
like image 730
Javel LP Avatar asked Mar 19 '15 17:03

Javel LP


2 Answers

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
like image 85
Sharvy Ahmed Avatar answered Sep 30 '22 19:09

Sharvy Ahmed


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"
....
like image 21
DickieBoy Avatar answered Sep 30 '22 20:09

DickieBoy