【问题标题】:Rails: undefined local variable or method `resource' Toastr RailsRails:未定义的局部变量或方法“资源”Toastr Rails
【发布时间】:2018-10-12 17:55:24
【问题描述】:

我想将 toastr-rails 添加到我的应用程序中,但现在出现以下错误。

# 的未定义局部变量或方法“资源”

在第一个应用程序中一切正常,我不知道这次出了什么问题......

宝石之间可能有冲突?

我的宝石文件:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.3.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'duktape'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'jquery-rails'
gem 'bootstrap-sass', '~>3.3.6'
gem 'devise', '~>4.2'
gem 'omniauth-google-oauth2'
gem 'toastr-rails', '~>1.0'
gem 'activeadmin'
gem 'active_skin'

用户.rb:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable,
         :omniauthable, omniauth_providers: [:google_oauth2]

  validates :fullname, presence: true, length: {maximum: 25}

  def self.from_omniauth(access_token)
    data = access_token.info
    user = User.where(email: data['email']).first

    unless user
    user = User.create(name: data['name'],
                     email: data['email'],
    password: Devise.friendly_token[0,20]
         )
     end
    user
end
end

Application_controller.rb

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname])
    devise_parameter_sanitizer.permit(:account_update, keys: [:fullname])
  end
end

_devisemes.html.erb

<% unless resource.errors.empty? %>
  <script type="text/javascript">
    <% resource.errors.full_messages.each do |value| %>
      toastr.error('<%= value %>')
    <% end %>
  </script>
<% end %>

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-5 toastr


    【解决方案1】:

    1.将您的 toastr gem 添加到您的 gemfile。

    gem 'toastr-rails', '~> 1.0'
    

    2。在您的 application.js 中,您需要添加 //= require toastr

    3.在您的 stylesheet.scss 中,您需要导入 toastr @import "toastr";

    4.然后在终端中运行bundle install

    5.然后转到查看/共享文件夹并创建一个局部视图文件并将其命名为_message.html.erb。在里面添加以下内容:

    <% unless flash.empty? %>
      <script type="text/javascript">
        <% flash.each do |key, value| %>
          <% type = key.to_s.gsub('alert','error').gsub('notice','success') %>
          toastr['<%= type %>']('<%= value %>')
        <% end %>
      </script>
    <% end %>
    

    基本上你是说如果 flash 不是空的,运行脚本里面的内容。在脚本中,您创建了一个名为type 的新变量,然后查找每个闪存,但toastr 没有noticealert,但toastr 确实支持errorsuccess 类型。所以我们可以从 flash 中获取一个密钥,然后将其更改为一个字符串并将 alert 替换为错误 ('alert','error') 并替换 ('notice','success') 然后我们说 toastr['&lt;%= type %&gt;']('&lt;%= value %&gt;') 类似于 toastr['error']('Full name can not be blank')

    6.现在你需要在你的所有设计视图中渲染这个局部视图。

    7 转到 application.html.erb 并在局部视图中呈现共享消息。像这样的东西:&lt;%= render shared/message %&gt;。如果您在 application.html.erb 中有一个,您很可能希望在导航栏部分视图下呈现它

    8.现在返回您的应用,退出并重新登录,您应该会收到通知消息。

    对于views/devise 文件夹中所有设计视图中的消息,您将 找到类似的东西&lt;%= devise_error_messages! %&gt; 你几乎会做同样的事情。只是有点不同。

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2014-07-06
      • 1970-01-01
      相关资源
      最近更新 更多