def create
@addpost = Post.new params[:data]
if @addpost.save
flash[:notice] = \"Post has been saved successfully.\"
redirect_to posts_path
e
flash.now
with render
is what you're looking for.
flash.now[:notice] = "Post can not be saved, please enter information."
render :new
Also instead of
flash[:notice] = "Post has been saved successfully."
redirect_to posts_path
you can just write
redirect_to posts_path, :notice => "Post has been saved successfully."
and it will do the same thing. It works only with redirect_to
though, not with render!
Hey this answer is super late but thought I'd add it for anyone that comes across it. Probably the most simple solution for what you want to achieve is to add required: true to all of the form inputs you want filled out. E.g
f.text_field :title, required: true, class: "whateverclassyouwant"
This way the form will ONLY be submitted if these fields have been filled in correctly and if not an error flash message will pop up on the field that it needs to be completed. The default flash messages that pop up can be custom styled also, Google how to do so.
This way you can remove the else redirect all together in your create method as it will never get to that point, and just have the if save, flash success etc.
Something like this should do what you want:
flash[:notice] = "Post can not be saved, please enter information."
render :new
UPDATE: You updated your question so I have to update my answer. Render is the right way to do this. However, it looks like you load some categories and some other collection of stuff in your new
method. Those same instance variables should be available to your create
method. The cleanest way to do this is put them into another method and have that method used as a before_filter
applied to both create
and new
. Something like this:
before_filter :load_stuff, :only => [:create, :new]
def load_stuff
@arr_select = { 1=>"One",2=>"Two" ,3=>"Three" }
@categories_select = Category.all.collect {|c| [ c.category_name, c.id ] }
end
Then your new
method is pretty much blank and calling render :new
in your create
method should work.