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

问题描述 投票:0回答:1

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

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

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

可能宝石之间存在冲突?

我的Gemfile:

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 %>
ruby-on-rails devise ruby-on-rails-5 toastr
1个回答
0
投票

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

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

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

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

然后在你的终端上运行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','error')替换警报并替换('notice','success')然后我们说toastr['<%= type %>']('<%= value %>')这就像toastr['error']('Full name can not be blank')

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

7转到application.html.erb并在局部视图中呈现共享消息。像这样:<%= render shared/message %>。如果您在application.html.erb中有一个,则很可能希望在导航栏局部视图下呈现此内容

8.现在回到您的应用程序,注销并重新登录,您应该收到通知消息。

对于views / devise文件夹中所有设计视图中的消息,你会发现像这样的<%= devise_error_messages! %>你几乎都做同样的事情。只是有点不同。

© www.soinside.com 2019 - 2024. All rights reserved.