Simple_form field not working - Ruby on Rails

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 
+5
source share
2 answers

His transition to the concept of creation is because you are not telling him anything else.

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

Creates a new Post instance with the parameters specified in the empty form. The new challenge does nothing in terms of verification. You want something like:

 @post = Post.new(params.require(:post).permit(:title, :category_id)) if @post.valid? && params[:previewButt] == "Continue to Preview your Post" .... 
+1
source

The simple_form initializer has a parameter that activates client-side validation. Regardless of client-side validation, you should also validate server-side validation (what you are currently doing). To implement client-side validation using simple_form, follow these steps:

configurations / Initializers / simple_form.rb

 config.browser_validations = true 
+15
source

Source: https://habr.com/ru/post/1215702/


All Articles