I'm following Michael Hartl's Rails 5 Tutorial online. I'm on the Toy App portion. I have a simple User scaffold in place. When I create a new user, I'm redirected to the show template, but the flash notice message content isn't being displayed.
The controller code is as follows:
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
end
end
end
The view code is here:
<p id="notice"><%= notice %></p>
The P element is being created and receiving the appropriate CSS styles, but the message isn't being passed to it.
Basically, I'm getting this for my output:
<p id="notice"></p>
I added this to the view:
<% if flash[:notice].blank? %>
<h1> flash notice is blank </h1>
<% end %>
...and I am getting the message that the flash notice is blank.
I'm on Rails 5.0.0.1 and Ruby 1.3.2. Thank you.
Okay, so this has to do with an edit that I made to the application_controller.rb file. The first time I tried to create a new user, I received an error:
ActionController::InvalidAuthenticityToken in UsersController#create
To remedy this, on the second line of my application_controller.rb file, I changed this:
protect_from_forgery with: :exception
to this:
protect_from_forgery with: :null_session
I guess this 'nullified' my session, so to speak, so certain session variables aren't getting passed, including flash messages?
Anyway, this change allowed me to create new users fine, but then I discovered that the flash notice messages weren't being displayed.
To remedy that, I changed :null_session back to :exception on the application_controller file, and then I added a new line to my users_controller.rb file that skips verification of an authentication token for new users. This probably isn't suitable for production, but for the sake of moving through the tutorial, it's a start:
class UsersController < ApplicationController
skip_before_action :verify_authenticity_token
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.