Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

ruby on rails - Dont recieve any messages from methods

I dont recieve any messages from controller. Method works, but without showing messages.

def create
      @post = Post.new(post_params)
      @post.user_id = current_user.id if user_signed_in?
        if @post.save
          redirect_to current_user, flash: {success: "Post was created"}
        else
          render :new, flash: {alert: "Some errors"}
        end
  end

And it doesnt matter will it be a happypass or not

question from:https://stackoverflow.com/questions/66063031/dont-recieve-any-messages-from-methods

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To use flash messages in controller when redirecting you need to use:

flash.alert = "Some errors"

or

flash[:alert] = "Some errors"

if you want render:

flash.now[:notice] = "Some errors"
render :new

But remember, use flash.now when rendering and flash when redirecting

Here you can search the details about this subject: https://www.rubyguides.com/2019/11/rails-flash-messages/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...