Rails 5 ..无闪光通知信息



我在网上跟随Michael Hartl的Rails 5教程。我在玩具应用部分。我有一个简单的用户支架到位。当我创建一个新用户时,我被重定向到show模板,但是没有显示flash通知消息内容。

控制器代码如下:

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

视图代码在这里:

<p id="notice"><%= notice %></p>

p元素正在被创建并接收适当的CSS样式,但是消息没有传递给它。

基本上,我的输出是这样的:

<p id="notice"></p>

我将这个添加到视图中:

<% if flash[:notice].blank? %>
  <h1> flash notice is blank </h1>
<% end %>

…我得到的信息是闪光通知是空白的。

我使用Rails 5.0.0.1和Ruby 1.3.2。谢谢你。

这与我对application_controller所做的编辑有关。rb文件。第一次尝试创建一个新用户时,我收到了一个错误:

ActionController::InvalidAuthenticityToken in UsersController#create

为了解决这个问题,在我的application_controller的第二行。我修改了这个:

protect_from_forgery with: :exception

:

protect_from_forgery with: :null_session

我想这"无效"我的会话,可以这么说,所以某些会话变量没有通过,包括闪光消息?

无论如何,这个更改允许我创建新用户,但后来我发现flash通知消息没有显示。

为了解决这个问题,我将application_controller文件上的:null_session更改回:exception,然后在users_controller中添加新行。跳过对新用户的身份验证令牌的验证的Rb文件。这可能不适合生产环境,但为了继续学习本教程,这是一个开始:

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token

最新更新