Ruby on Rails: How would i stay on the same page if the post is not saved?

前端 未结 3 802
小鲜肉
小鲜肉 2021-02-19 03:59
def create
    @addpost = Post.new params[:data]
    if @addpost.save
        flash[:notice] = \"Post has been saved successfully.\"
        redirect_to posts_path
    e         


        
相关标签:
3条回答
  • 2021-02-19 04:17

    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!

    0 讨论(0)
  • 2021-02-19 04:22

    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.

    0 讨论(0)
  • 2021-02-19 04:25

    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.

    0 讨论(0)
提交回复
热议问题