Rails:未定义的局部变量或方法`resource'Toastr Rails

时间:2018-10-12 17:55:24

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

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

#<#:0x854eb18>的未定义局部变量或方法`resource'

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

宝石之间可能存在冲突?

我的宝石文件:

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'

User.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 %>

1 个答案:

答案 0 :(得分:0)

1。将您的敬酒宝石添加到您的宝石文件中。

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的新变量,然后查找每一个flash,但是toastr没有noticealert,但是toastr确实支持类型error和{ {1}}。这样我们就可以从闪存中获取密钥,然后将其更改为字符串,并用错误success替换警报,并替换('alert','error'),然后说出('notice','success'),就像toastr['<%= type %>']('<%= value %>')

6。现在,您需要在所有设计视图中渲染此局部视图。

7转到application.html.erb并在部分视图中呈现共享消息。像这样的东西:toastr['error']('Full name can not be blank')。如果您在application.html.erb

中有一个视图,则很可能要在导航栏局部视图下呈现此视图。

8。现在返回您的应用,注销并重新登录,您将收到通知消息。

对于views / devise文件夹中所有devise视图中的消息,您将 找到类似<%= render shared/message %>的东西,您几乎会做同样的事情。只是有些不同。