小编Eye*_*dic的帖子

如何执行检查是否启用了Rails 5缓存?

在Rails 5中,可以启用/禁用缓存:

rails dev:cache
=> Development mode is now being cached.

rails dev:cache
=> Development mode is no longer being cached.
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果启用或禁用了缓存,是否有一种方法可以使应用程序从中获取信息?就像是:Rails.cache.enabled?

我知道可以检查file的存在tmp/caching-dev.txt,但是我正在寻找更高级别的东西。

caching ruby-on-rails ruby-on-rails-5

2
推荐指数
1
解决办法
989
查看次数

PG::DatatypeMismatch:错误:AND 的参数必须是布尔类型,而不是整数类型

我使用 MySQL 在 Rails 中开发了一个基本应用程序。我正在转换为 PostgreSQL 数据库。我完成了数据库设置,除了有many to many关系的查询之外,大多数查询都可以工作。

我的模型看起来像这样:

User has_and_belongs_to_many :locations

Location has_and_belongs_to_many :users
Run Code Online (Sandbox Code Playgroud)

但是,当我执行这行代码时,出现以下错误:

location = current_user.locations.find_by(cookies[:current_location])
Run Code Online (Sandbox Code Playgroud)

产生这个错误:

PG::DatatypeMismatch: ERROR:  argument of AND must be type boolean, not type integer
LINE 1: ...n_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $...
                                                         ^
: SELECT  "locations".* FROM "locations" INNER JOIN "locations_users" ON "locations"."id" = "locations_users"."location_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $2
Run Code Online (Sandbox Code Playgroud)

提前致谢。

ruby-on-rails

2
推荐指数
1
解决办法
1455
查看次数

Ruby:为什么所有坐标都在一个数组中更新?

我正在尝试更改特定的坐标,但是数组正在更新所有坐标。

目标是将fixed属性更改为单个坐标。

class Case
  attr_accessor :fixed

  def initialize
    self.fixed = false
  end

  def fixed?
    !!fixed
  end
end

def display(arr)
  5.times do |x|
    5.times do |y|
      print arr[x][y].fixed?
      print ' '
    end

    puts
  end
end

# Defining array
arr = Array.new(5){ Array.new(5, Case.new) }

# Displaying the arrays
display(arr)

# Changing value of a single coord
arr[2][3].fixed = true

# Displaying the arrays
display(arr)

Run Code Online (Sandbox Code Playgroud)

这是第一个显示呼叫的结果

false false false false false 
false false false false false 
false false false false …
Run Code Online (Sandbox Code Playgroud)

ruby

2
推荐指数
1
解决办法
45
查看次数

如何删除 React Web 应用程序中左上角的计时器计数器

我的反应应用程序左上角的时间隐藏了我的应用程序中的内容,我不知道如何删除它。

在此处输入图片说明

这是我截取的Gemfile. 与时间无关。

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

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

2
推荐指数
2
解决办法
1164
查看次数

Bootstrap 行和列响应式

使用 bootstrap 的网格,是否可以构建像这样的响应式东西?

我已经把它画出来了

这就是我到目前为止所尝试的。

<div class="row">
    <div class="panel panel-default profile_header">
        <div class="panel-body">
            <% for user in @users %>
                <div class="col-xs-5 col-md-3">
                    <%= image_tag(user.avatar.url(:medium), :class => "avatar_profile") %>
                </div>
                <div class="col-xs-7 col-md-9">
                    <h2 class="profile_name"><%= user.name %></h2>
                    <div class="row">
                        <div class="col-xs-12 col-md-12">
                            <h2 class="profile_bio"><%= user.bio%></h2>
                        </div>
                    </div>
                </div>
            <% end %>
        </div>
    </div>
</div> 
Run Code Online (Sandbox Code Playgroud)

但对于移动设备,我希望紫色列弹出并显示在下面。现在的内容太拥挤了,将其分成自己的全宽框会好得多。这可能吗?

css responsive-design twitter-bootstrap

1
推荐指数
1
解决办法
1万
查看次数

如何获取用|分隔的字符串 要么 ,

假设我有一个字符串,如| b | c | d ...或者像a,b,c,d ......我想提取abc和d并将其推入数组中.这样做的有效方法是什么?

因此,输入是a|b|c|d或者a,b,c,d输出应该是[a,b,c,d].

ruby string

1
推荐指数
1
解决办法
66
查看次数

通过调用控制器方法进行Rails数据库播种

我想用许多“联盟”模型作为测试数据库的种子。在我的Leagues_controller中,create方法不仅创建联赛,而且还调用与每个联赛模型相关的许多初始化方法。据我所知,当我在db / seeds.rb中进行方法调用时,实际上并没有调用控制器方法(通过运行我当前在seed.rb中拥有的内容并注意各种方法来确认该方法)。初始化方法未运行)。

有什么方法可以直接在seeds.rb中调用控制器的create方法,从而不必重复代码?

这是一个例子:我想建立100个联赛。创建联赛后,我会以帮助者的方式创建球员和球队以与之并驾齐驱。我只想打电话给League.create 100次,而不用担心也要创建团队和球员。这是我的代码:

db / seeds.rb:

number = 1
number_of_teams = 8
100.times {
    league_name = "Seed" + number.to_s
    if number > 33
        number_of_teams = 10
    elsif number_of_teams > 67
        number_of_teams = 12
    end
    # Needs to be modified, we want to call the controller method
    LeaguesAndTeams::League.create(name: league_name, number_of_teams: number_of_teams)
    number += 1
}
Run Code Online (Sandbox Code Playgroud)

还有我的Leagues_controller(我简化了代码以便于阅读):

def create
        @league = LeaguesAndTeams::League.new(league_params)
        puts "In the league create method"
        respond_to do |format|
            if @league.save
                # Initialization: I know this isn't …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails seeding

1
推荐指数
1
解决办法
642
查看次数

Rails 连接多个条件的查询

我正在尝试查询 2 个表、凭据和集成。

我想检索每一条凭证记录WHERE user_id == current_user.id。我还想检索每个集成记录WHERE name == "Twitter"

下面的查询运行并输出[[4, "Twitter"][3,"YouTube"]]

Credential.joins(:integration).where(user_id: current_user.id).pluck(:id, :name)
Run Code Online (Sandbox Code Playgroud)

下面的查询应该运行并生成[4, "Twitter"],但它却产生错误。

Credential.joins(:integration).where(user_id: current_user.id).where(integration: {name: 'Twitter'}).pluck(:id, :name)

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "integration"
LINE 1: ...ration_id" WHERE "credentials"."user_id" = $1 AND "integrati...
Run Code Online (Sandbox Code Playgroud)

最终,我试图将我在其余代码中使用的集成列入白名单。如果我在 joins 参数中使用复数,Rails 会拒绝查询.joins(:integrations)

postgresql join ruby-on-rails rails-activerecord

1
推荐指数
1
解决办法
4128
查看次数

如何将哈希键值对作为单独的参数传递?

我希望能够快速将哈希的键值对作为参数放入方法中。

例如,对于哈希:

test_hash = {a: "1", b: "2"}
Run Code Online (Sandbox Code Playgroud)

我希望能够将其传递到如下方法中:

method(test_hash.do_something)
Run Code Online (Sandbox Code Playgroud)

实现:

method(a: "1", b: "2")
Run Code Online (Sandbox Code Playgroud)

ruby

1
推荐指数
1
解决办法
627
查看次数

Profile:Class 的未定义方法“before_action”

下午好,美丽的社区,我有以下问题:即使方法定义明确,我也有这个错误,如果有人能帮我一把。我希望用户注册时自动为该用户创建个人资料

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

  has_many :patients, dependent: :destroy
  has_one :profile, dependent: :destroy
  

  after_create :set_profile

  def set_profile
    self.profile = Profile.create()  
  end      

end

class Profile < ApplicationRecord
  before_action :set_profile

  belongs_to :user
  
  private

  def set_profile
    @profile = (current_user.profile ||= Profile.create)
  end

end

class ProfilesController < ApplicationController
  before_action :set_profile

  def show
  end

  def edit
  end

  def update
  end

  private

  def set_profile
    @profile …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

1
推荐指数
1
解决办法
35
查看次数