我试图传递一个变量来渲染像这样:
def monitor
@peripheries = Periphery.where('periphery_type_name=?','monitor')
render 'index', type: 'Monitor'
end
Run Code Online (Sandbox Code Playgroud)
这里我想在索引视图中使用'type'变量,它看起来像这样:
<%= render 'some_partial', periphery_type: type %>
Run Code Online (Sandbox Code Playgroud)
这也是一些东西.但我想使用那个'type'变量
我试图使用Devise gem在RoR应用程序中获取最后一次登录日期时间.在我的一个迁移文件中,我注意到了# t.datetime :last_sign_in_at其他字段:
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip
Run Code Online (Sandbox Code Playgroud)
所以问题是:如何为我的用户激活此字段,以便稍后在我的应用程序中调用它?我应该取消注释吗?
在带有 Postgres 的 Rails 应用程序中,我有一个用户、工作和关注者连接表。我想选择特定用户未关注的作业。但也包括连接表中没有行的作业。
users:
id: bigint (pk)
jobs:
id: bigint (pk)
followings:
id: bigint (pk)
job_id: bigint (fk)
user_id: bigint (fk)
Run Code Online (Sandbox Code Playgroud)
sandbox_development=# SELECT id FROM jobs;
id
----
1
2
3
(3 rows)
Run Code Online (Sandbox Code Playgroud)
sandbox_development=# SELECT id FROM users;
id
----
1
2
Run Code Online (Sandbox Code Playgroud)
sandbox_development=#
SELECT id, user_id, job_id FROM followings;
id | user_id | job_id
----+---------+--------
1 | 1 | 1
2 | 2 | 2
(2 rows)
Run Code Online (Sandbox Code Playgroud)
# jobs
id
----
2
3
(2 rows) …Run Code Online (Sandbox Code Playgroud) class Person
validates_numericality_of :identity_id, :message => "^Person field definition id must be an integer"
end
Run Code Online (Sandbox Code Playgroud)
现在,^在消息属性中使用 的效果是什么?
我正在尝试测试这个类 ( Swiftris/Swifteris/Array2D.swift):
class Array2D<T> {
let rows: Int
let columns: Int
var array: Array<T?>
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
array = Array<T?>(count:rows * columns, repeatedValue: nil)
}
subscript(column: Int, row: Int) -> T? {
get {
return array[(row * columns) + column]
}
set {
array[(row * columns) + column] = newValue
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这个简单的测试 ( Swiftris/SwiftrisTests/Array2DTest.swift):
import UIKit
import XCTest
class Array2DTest: XCTestCase {
override func setUp() {
super.setUp() …Run Code Online (Sandbox Code Playgroud) 我收到这个错误:
method_missing':
expect在示例组(例如adescribe或context块)上不可用.它只能从各实施例(例如内it的块),或从该中的示例(例如范围运行构建体before,let等).(:: RSpec的核心:: ExampleGroup :: WrongScopeError)
对于此规范:
describe 'canary test' do
expect(true).to be true
end
Run Code Online (Sandbox Code Playgroud) 我有一个帮助方法,为资源生成"crud"链接.现在它只处理编辑和销毁.但我想重构它,以便new在传递模型类而不是实例时创建链接.
期望的输出:
> crud_links_for_resource(Product)
> { :new => '<a href="/products/new">Create a new product.</a>'}
Run Code Online (Sandbox Code Playgroud)
检查变量是模型类还是实例的最佳方法是什么?我想过使用duck typing(resource.respond_to? :new_record?)但是有更好的方法吗?
module PermissionsHelper
# Generates a hash of links to edit or destroy a resource
# @param [ActiveModel] resource
# @param [Actions] a list of crud actions to create links to
# @param [Hash] kwargs optional hash to pass to link to
# @option kwargs [String] :controller - controller name to use.
# Otherwise a guess is performed based on the …Run Code Online (Sandbox Code Playgroud) 我有一个产品模型,我需要在_form视图中写入,管理员想要插入的产品的数量.我有另一个带有Supply(产品数量)的表,所以在我的产品表中我没有属性数量,但我只有supply_id(链接我的两个产品和供应表)
由于我的产品表中没有数量,因此我在Product上使用了虚拟属性.
我不得不改变新的视图和编辑产品的原因在新的我希望字段数量但在编辑中我不想要(因为我使用另一个视图来做这个)所以,我删除了部分_form并创建了单独的视图.另外,我必须在产品的控制器中设置如果我想更新产品,我必须调用set_quantity回调,因为我必须插入一个"假"值来填充params[:product][:quantity].这是因为我在产品模型中的数量虚拟字段上设置了验证状态为true.我想知道,如果所有这些故事都是正确的(它有效,但我想要一个关于这个故事的编程设计的建议.因为我不喜欢这样一个事实,即当我有一个假值来填充数量字段时更新产品)
控制器:
class ProductsController < ApplicationController
include SavePicture
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :set_quantita, only: [:update]
....
def set_quantita
params[:product][:quantita]=2 #fake value for the update action
end
....
end
Run Code Online (Sandbox Code Playgroud)
模型:
class Product < ActiveRecord::Base
belongs_to :supply ,dependent: :destroy
attr_accessor :quantita
validates :quantita, presence:true
end
Run Code Online (Sandbox Code Playgroud)
如果有更好的方法来填写param[:product][:quantity]更新操作,你能说我吗?因为我不喜欢我给它的值2的事实.谢谢.
我在div中隐藏了一个无序列表.div有一个'.feed-label'类,当div跳过时我正在显示ul.
我的问题是,当盘旋时,所有其他元素也都显示出来,我只想要悬停在其中的那个元素来显示.
我不知道如何使用$(this).
$('.feed-label').hover(function() {
$('.article-interactive-buttons').toggleClass('hide');
});
Run Code Online (Sandbox Code Playgroud) 我有2个模特,促销和用户.
Promo belongs_to :user
User has_many :promos
Run Code Online (Sandbox Code Playgroud)
在我的路由中,我有嵌套资源:
devise_for :users
resources :users do
resources :promos
end
Run Code Online (Sandbox Code Playgroud)
我有一个表格来制作新的宣传片 simpleform
<%= simple_form_for [current_user, @promo] do |f| %>
<%= f.input :title, label: "Título de la promoción" %>
<%= f.input :image, label: "Imágen de la promoción", class: "" %>
<%= f.input :description, :as => :text, label: "Descripción de la promoción", :input_html => {class: "materialize-textarea"} %>
<%= f.input :title, label: "Pabrasecreta", class: "validate", input_html: {length: "10"} %>
<%= f.submit "Crear Palabra secreta" , class: "right …Run Code Online (Sandbox Code Playgroud) ruby ×3
rspec ×2
devise ×1
javascript ×1
jquery ×1
postgresql ×1
swift ×1
tdd ×1
this ×1
xcode ×1