I have a presentation form in a RoR application built with simple_form. When the fields are empty, the application continues to the next step without causing errors or warnings. Fields must be default required: true ; but even writing it by hand does not work.
The application has 3 steps: NewPost (new view) β Preview (create view) β Message.
It would be more clear if you deduce 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 Look (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 %>
Please note that the problem is not saving, the model definitions work fine, the problem is only in simple_form.
class Post < ActiveRecord::Base belongs_to :category validates :title, presence: true validates :category_id, presence: true end
SOLUTION thanks to the hint of DickieBoy :
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