我在Rails 4中遇到了一个复杂的问题,我将在下面介绍.我使用简单的形式和awesome_nested_fields宝石.
我的应用程序中有一些带有一些字段的事件
这是我在working_nested_attributes实现之前的工作event_controller参数:
private
def event_params
params.require(:event).permit(:title, :description, :type_id, :price, :program,
:start_date, :end_date, :image, category_ids: [])
end
Run Code Online (Sandbox Code Playgroud)
现在我想在我的活动中添加一些发言者,并让用户决定每个活动他想要多少个发言者.因此,根据他们的文档,我添加了嵌套字段gem并使扬声器成为嵌套字段.
Event.rb
class Event < ActiveRecord::Base
has_many :speakers
accepts_nested_attributes_for :speakers, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
Speaker.rb
class Speaker < ActiveRecord::Base
belongs_to :event
end
Run Code Online (Sandbox Code Playgroud)
添加扬声器(在我的添加事件simple_form_for中):
<%= f.nested_fields_for :speakers do |f| %>
<fieldset class="item">
<%= f.label :name %>
<%= f.text_field :name %>
<a href="#" class="remove">remove</a>
<%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %>
</fieldset>
<% end %>
Run Code Online (Sandbox Code Playgroud)
更新强参数的控制器:
private
def event_params
params.require(:event).permit(:title, :description, :type_id, :price, …Run Code Online (Sandbox Code Playgroud) 有没有办法创建一个黑名单,禁止某些用户在注册过程中通过过滤器通过参数注册?
这个想法是防止使用可删除电子邮件地址(例如10分钟邮件)的用户注册.
我知道我可以指定某些字段来查询数据库pluck.
ids = Item.where('due_at < ?', 2.days.from_now).pluck(:id)
Run Code Online (Sandbox Code Playgroud)
但是我想知道,是否有一种方法可以指定某些字段,我希望避免从数据库中查询.某种反拔?
posts = Post.where(published: true).do_not_lookup(:enormous_field)
Run Code Online (Sandbox Code Playgroud) 我正在尝试从生产服务器访问我的 S3 存储桶。在开发中一切正常,但是在我的 prod 控制台中我得到:
Aws::S3::Errors::AuthorizationHeaderMalformed: The authorization header is malformed; the Credential is mal-formed; expecting "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request".
Run Code Online (Sandbox Code Playgroud)
我的代码:
class AwsFileDownloader
def initialize(args)
@s3 = Aws::S3::Client.new
@aws_file_path = ...
end
def get_file
temp_file = File.new('temp.csv', 'r+')
@s3.get_object({bucket: Rails.application.secrets.aws_bucket, key: @aws_file_path}, target: temp_file)
...
end
end
Run Code Online (Sandbox Code Playgroud)
我的 aws 初始化程序(在两种环境中似乎都可以正常工作):
require 'aws-sdk'
Aws.config.update({
region: Rails.application.secrets.aws_region,
credentials: Aws::Credentials.new(Rails.application.secrets.access_key_id, Rails.application.secrets.secret_access_key)
})
Run Code Online (Sandbox Code Playgroud)
感谢任何建议!
不明白为什么我不能让任何 jQuery 插件函数(引导程序、日期选择器等)工作:
$(...).datepicker()或$(...).modal()回应... is not a function in Rails 6
一些注意事项:
应用程序.js:
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require("parsleyjs")
require("bootstrap-datepicker")
import '../stylesheets/application'
import './bootstrap_custom.js'
import './ru.js'
Run Code Online (Sandbox Code Playgroud)
包.json:
{
"name": "joy",
"private": true,
"dependencies": {
"@rails/actioncable": "^6.0.0-alpha",
"@rails/activestorage": "^6.0.0-alpha",
"@rails/ujs": "^6.0.0-alpha",
"@rails/webpacker": "^4.0.7",
"bootstrap": "4.3.1",
"bootstrap-datepicker": "^1.9.0",
"jquery": "^3.4.1",
"parsleyjs": "^2.9.1",
"popper.js": "^1.16.0",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.8.2"
}
} …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中有一个Location模型,但后来我退了一步Git,当时我还没有创建模型,然后经过一段时间在另一个分支上工作,我意识到模型已经无处可去了,它是仍然在我的架构中.我试图摆脱它,但它仍然突然冒出来,虽然我的应用程序中没有添加位置迁移文件.什么是摆脱它并清理schema.rb的最佳方法?
更新1
运行
rails销毁模型位置
给我
invoke active_record
remove /Users/denis/Desktop/migration/templates/create_table_migration.rb
remove app/models/location.rb
invoke test_unit
remove test/models/location_test.rb
remove test/fixtures/locations.yml
Run Code Online (Sandbox Code Playgroud)
并且我可以无限期地运行它,它将始终给出相同的结果
运行此迁移:
class DropLocationsTable < ActiveRecord::Migration
def self.up
drop_table :locations
end
end
Run Code Online (Sandbox Code Playgroud)
在rake db:migrate之后给出0结果再次出现在schema.rb中的Locations
更新2
rails db
SQLite version 3.7.12 2012-04-03 19:43:07
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT * FROM locations;
sqlite>
Run Code Online (Sandbox Code Playgroud) 我正在遵循基本的Angular教程,需要在其中包含一个JSON文件.我用Yeoman启动了我的应用程序,它正在咕噜咕噜地运行.
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});
});
Run Code Online (Sandbox Code Playgroud)
但是,当我转到localhost:9000时,我收到一堆控制台错误:
ReferenceError: $http is not defined
at new PhoneListCtrl (http://localhost:9000/scripts/controllers/main.js:17:3)
at invoke (http://localhost:9000/bower_components/angular/angular.js:3000:28)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:3012:23)
at http://localhost:9000/bower_components/angular/angular.js:4981:24
at http://localhost:9000/bower_components/angular/angular.js:4560:17
at forEach (http://localhost:9000/bower_components/angular/angular.js:137:20)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4545:11)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4191:15)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:4194:13)
at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:4096:30)
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
在我的SQLite数据库迁移到开发服务器上的Postgres后,我得到了
PG::UndefinedFunction: ERROR: function strftime(unknown, date) does not exist
Run Code Online (Sandbox Code Playgroud)
有没有我可以使用的功能来保持我的应用程序的功能?
编辑:尝试使用to_char,但失败了.特别是,我不明白如何在诸如此类的结构中实现它
def self.query_by_year_month(y, m)
where("strftime('%Y', date) = ? and strftime('%m', date) = ?", y, m)
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
我只是想确定这是否可行:
class AddUpdatedById < ActiveRecord::Migration
def change
add_column :clients, :updated_by_id, :integer
add_column :contacts, :updated_by_id, :integer
add_column :court_agencies, :updated_by_id, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
这样我就可以保存迁移.因为如果我将逐个迁移它们,我认为这将需要更多时间并且使我的迁移陷入困境.所以你怎么看?
是否有可能或者我应该逐一做同样的事情?
我有以下方法:
def self.fetch_by_subcat(subcat_id, state)
where(state: state).joins(:subcategories).where("subcategory_id = ? AND published = ?", subcat_id, true)
end
Run Code Online (Sandbox Code Playgroud)
问题是,如果我的状态为空,我会返回一个空数组.所以我想将我的state参数设置为默认值,例如'any',它将简单地跳过where(state ...)查询并直接进入连接.所以像
def self.fetch_by_subcat(subcat_id, state='any')
where(state: state).joins...
end
Run Code Online (Sandbox Code Playgroud)
有办法吗?试图避免if/else.
ruby ×3
activerecord ×1
amazon-s3 ×1
angularjs ×1
devise ×1
email ×1
gruntjs ×1
javascript ×1
jquery ×1
json ×1
node.js ×1
postgresql ×1
security ×1
web ×1