嗨,我需要一些帮助,为我的页面模板创建自定义css文件.关于这个问题有很多主题,但我阅读的每个帖子都会得到更多信息和更多信息.
我为二十四个主题创建了一个子主题,并添加了一个页面模板.如何将自定义css添加到此模板.我发现这个代码添加到了child-theme的functions.php中,用我的css选择了合适的类.但是我如何以及在哪里上课?我读到我必须将该类添加到header.php中的body标签,但我不确定.这是正确的方法吗?
if (is_page_template( 'mytemplate.php' )){
$classes[] = 'myclass';
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Rails项目(Rails版本4.2.3).我创建了一个User
和Task
模型,但在创建过程中没有包含它们之间的任何关联.现在我希望一个user
人有tasks
一个task
属于一个人user
.
通过rails g migration AddUserToTask user:belongs_to
从这个线程
我能插入任务表的外键USER_ID.但是如何添加has_many
迁移呢?我更新了User
模型:
class User < ActiveRecord::Base
has_many :customers
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何编写迁移.到目前为止我写了这个:
class addTasksToUser < ActiveRecords::Migration
def change
update_table :users do |t|
t.has_many :tasks
end
add_index :users, taks_id
end
end
Run Code Online (Sandbox Code Playgroud)
但是rake db:migrate
没有采取任何行动.这是建立has_many
关系的正确方法吗?
activerecord ruby-on-rails associations has-many ruby-on-rails-4
如果验证失败或缺少参数,我想从我的控制器返回400 - bad request
.所以在我的控制器中如果有
if params["patch"].nil? then
raise ActionController::BadRequest.new( "The Json body needs to be wrapped inside a \"Patch\" key")
end
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序控制器中捕获此错误:
rescue_from ActionController::BadRequest, with: :bad_request
def bad_request(exception)
render status: 400, json: {:error => exception.message}.to_json
end
Run Code Online (Sandbox Code Playgroud)
但似乎我无法在提升时添加自定义消息ActionController::BadRequest
.因为当传递无效的主体时,响应仅是,{"error":"ActionController::BadRequest"}
而不是我提供的哈希.
在控制台中,我得到了相同的行为. raise ActiveRecord::RecordNotFound, "foo"
确实提高了ActiveRecord::RecordNotFound: foo
.
但raise ActionController::BadRequest, "baa"
结果是
ActionController::BadRequest: ActionController::BadRequest
如何向BadRequest
异常添加自定义消息?
我重构了我的rails应用程序,对于我创建控制器的每个子资源都是相应的命名空间.
api/v1/app/controller/manager.rb
api/v1/app/controller/manager/user.rb
api/v1/app/controller/manager/controller.rb
api/v1/app/controller/admin.rb
api/v1/app/controller/user.rb
api/v1/app/controller/controller.rb
Run Code Online (Sandbox Code Playgroud)
manager命名空间下的用户资源的类定义如下所示
class Api::V1::Manager::UserController < ApplicationController
Run Code Online (Sandbox Code Playgroud)
该控制器可通过routes.rb访问:
resources :manager , only: [:show ] do
resources :user, only: [:index], controller: 'manager/user'
end
Run Code Online (Sandbox Code Playgroud)
产生
/api/v1/manager/:manager_id/user(.:format) api/v1/manager/manager#index {:format=>"json"}
Run Code Online (Sandbox Code Playgroud)
这些模型都在
app/models/manager.rb
app/models/user.rb
Run Code Online (Sandbox Code Playgroud)
当我想现在访问控制器Manager
内的模型api/v1/app/controller/manager/user.rb
或api/v1/app/controller/manager.rb
例如
class Api::V1::ManagerController < ApplicationController
def index
Manager.find(...)
end
end
class Api::V1::Manager::UserController < ApplicationController
def index
Manager.find(...)
end
end
Run Code Online (Sandbox Code Playgroud)
我得到这些错误
{"error":"uninitialized constant Api::V1::Manager::UserController::Manager"}%
{"error":"uninitialized constant Api::V1::Manager::Manager"}%
Run Code Online (Sandbox Code Playgroud)
调用由正确的控制器处理:
Processing by Api::V1::Manager::UserController#index as JSON
Run Code Online (Sandbox Code Playgroud)
解决方案是在调用时使用双冒号前缀
`::Manager.find(...)`.
Run Code Online (Sandbox Code Playgroud)
我可以使用所有其他型号Admin.find(...)
或Controller.first
正常使用.只有Manager.find(..) …
每次创建对象时,我都想将它排入 Redis 队列以检查某些属性。如何将创建的对象直接作为参数添加到回调中?所以我的 redis 工作会做这样的事情:
class FurtherProcessCarJob
#....
def self.perform(order)
puts order.id
end
end
Run Code Online (Sandbox Code Playgroud)
而在模型中
after_create Resque.enqueue FurtherProcessCar, #self
Run Code Online (Sandbox Code Playgroud)
可以将一个方法挂接到回调上,然后再次查找汽车并将对象排入队列,但是否可以直接执行此操作?
我想在PostgreSQL中的数据库内创建一个简单的表。从我有的文档中CREATE TABLE will create a new, initially empty table in the current database. The table will be owned by the user issuing the command.
使用此命令
CREATE TABLE *table_name*;
Run Code Online (Sandbox Code Playgroud)
我以为我得到了一个新的空表ERROR: syntax error at or near ";"
。但是psql抛出了。当我使用一个空的参数列表时,例如:
CREATE TABLE *table_name*();
Run Code Online (Sandbox Code Playgroud)
psql告诉我该表是通过以下方式创建的
postgres=# create table *table_name*();
CREATE TABLE
Run Code Online (Sandbox Code Playgroud)
但是\ l显示未显示新创建的表。而且也无法使用psql -d table_name -U user_name登录。有人可以帮忙吗?
由于 rspec 3.5 请求规范用于测试控制器行为以及路由测试以及视图与内容的正确呈现。最后一部分让我有点烦恼,因为我不明白视图规范中的内容与请求规范中的内容之间的细线。
在relishapp 上,我发现了这篇文章:
expect(response.body).to include("Widget was successfully created.")
这诱使我在请求测试中包含以下内容:
describe "Index page" do
....
it 'includes a link to cars_parts' do
get "/car_overview"
expect(response.body).to include("car_parts_path")
end
....
end
Run Code Online (Sandbox Code Playgroud)
此测试失败。在视图我使用link_to
的 car_parts url_helper
。失败的测试转储了整个 response.body 字符串,我看到了car_parts_path
但 rspec 没有看到它。我如何更改我的测试以使其在不使用水豚的情况下通过,因为它现在只能用于功能规格。我到底做对了还是应该在其他地方进行这种测试?
我想实现以下逻辑:
我们通过普通视图与我们的数据库进行交互.通过视图,可以对数据库对象执行基本的CRUD操作.每当现在一个特定的布尔值(对于任何记录)设置为true时,我想将此记录传递给REST API控制器,然后构建一个要发送给已知客户端的http字符串.
在模型类中,我通过after_update
回调观察所需的值
private
def check_boolean
@listofusers ||= Array.new
if self.boolean_changed?
@listofusers.push(self)
end
#redirect_to send_updates_path
#redirect_to :controller =>'api/users',:action=>"send_updates"
end
Run Code Online (Sandbox Code Playgroud)
这是我包含在我的路线中routes.rb
以获取指定的助手
get 'send_updates', to: 'api/user#send_updates', as: :send_updates
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
NoMethodError (undefined method `redirect_to' for #<User:0x00000005abc778>):
Run Code Online (Sandbox Code Playgroud)
重定向到控制器操作的正确方法是什么?提前谢谢了.
我在为FactoryGirl的多态关联设置工厂时遇到问题.我的模型和工厂设置如下所示:
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
class Customer < ActiveRecord::Base
has_one :address, as: :addressable
end
factory :address do
village Faker::Address.city
upazilla Faker::Address.street_name
ward Faker::Address.street_address
district Faker::Address.state
association :addressable
end
factory :customer ,class: Customer do
recharge_token 10
date_of_birth Faker::Date.backward(100)
manager
after(:create) do |customer|
customer.address = create(:address, addressable: customer)
#create( :address, addressable: customer)
end
end
Run Code Online (Sandbox Code Playgroud)
测试套件打破以下错误消息
vendor/cache/ruby/2.2.0/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:12:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
* address - Factory not registered: addressable (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
这正是我的问题,但不幸的是他的解决方案对我不起作用.谢谢大家的时间!
我的模特有以下关系
class User < ActiveRecord::Base
has_many :controllers
end
class Controller < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
Controller
的布尔值称为is_active
。
如果属于特定用户对象的所有控制器对象均为is_active
false,我想提出一个例外。
不幸的是,我很难将这句话变成代码。
# if for all controllers is_active false is met, raise exception
# ~> need to find one controller which is active
array = []
User.find(id).controllers.each do |c|
array << c.is_active
end
unless array.include?('true')
raise ...
end
Run Code Online (Sandbox Code Playgroud)
感觉有更多的rubisch
方法可以编写此代码。
我正在使用 Spring 和 Gradle 来构建一个宁静的 API。由于此 API 与 PostgreSQL 数据库配合使用,因此我还使用 JPA 连接此数据库并与之交互。我使用这个 spring.io 指南来开发基本应用程序并能够使用 JPA,我添加了一些更多的依赖项。该应用程序现在总共有 29MB 大小,这对我来说有点太大了。到目前为止,API 功能没有什么比一些基本的 CRUD 操作更复杂,因此我尝试通过以下方式减小整体大小:
使用gradle lint 插件自动处理我未使用的依赖项,但运行gradle buildJava fixGradleLint
返回
任务 :fixGradleLint 通过 lint 检查 0 次违规;无需更正 更正了 0 个棉绒问题
在 gradle 构建文件中,我添加了一个配置块,我在未使用的依赖项上使用了排除组或模块,例如:
configurations {
implementation {
exclude group: 'org.yaml', module: 'snakeyaml'
exclude group: 'net.bytebuddy'
exclude group: 'org.objenesis', module: 'objenesis'
exclude group: 'org.mockito'
// exclude module: 'spring-boot-starter-logging'
}
}
Run Code Online (Sandbox Code Playgroud)这产生的效果是,例如Mockito从提供的依赖项列表中消失,gradle dependiencies
但它们的排除项根本没有减少应用程序的大小。
如何有效地减小应用程序的大小?也许我可以摆脱弹簧启动器,因为它们似乎可以拉入包括我的咖啡杯在内的所有东西;)。但是,我没有找到如何执行此操作的方法。我会为每一个消失的 MB 感到高兴。
下面我发布了我的 …
ruby ×3
rspec-rails ×2
activerecord ×1
associations ×1
callback ×1
create-table ×1
css ×1
factory-bot ×1
gradle ×1
has-many ×1
java ×1
php ×1
postgresql ×1
redis ×1
rspec ×1
spring ×1
spring-boot ×1
wordpress ×1