当我意识到你应该在视图中使用帮助器时,我需要一个帮助器在我的控制器中,因为我正在构建一个JSON对象来返回.
它有点像这样:
def xxxxx
@comments = Array.new
@c_comments.each do |comment|
@comments << {
:id => comment.id,
:content => html_format(comment.content)
}
end
render :json => @comments
end
Run Code Online (Sandbox Code Playgroud)
我怎样才能访问我的html_format助手?
我已将我的一个应用程序从Rails 4.2.6升级到Rails 5.0.0.该升级指南说,是自动加载功能在生产中默认情况下禁用现在.
现在我总是在生产服务器上出错,因为我在application.rb文件中加载了所有带有自动加载的lib 文件.
module MyApp
class Application < Rails::Application
config.autoload_paths += %W( lib/ )
end
end
Run Code Online (Sandbox Code Playgroud)
现在,我已经设定了config.enable_dependency_loading,true但我想知道是否有更好的解决方案.必须有一个原因,默认情况下在生产中禁用自动加载.
你如何or在Rails 5 ActiveRecord中进行查询?此外,是否有可能链or与where在ActiveRecord的查询?
ruby activerecord ruby-on-rails rails-activerecord ruby-on-rails-5
这个代码在rails 5中
class PagesController < ApplicationController
def action
render nothing: true
end
end
Run Code Online (Sandbox Code Playgroud)
导致以下弃用警告
DEPRECATION WARNING: :nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body.
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我正在使用Rails 5
我在模型用户中添加了新的字段用户名.
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up).push(:username)
end
end
Run Code Online (Sandbox Code Playgroud)
在注册期间显示错误:未定义的方法`for'for#您的意思是?叉子
跟踪:
NoMethodError(未定义的方法`为'for#你的意思是?fork):
app/controllers/users/registrations_controller.rb:7:in `configure_permitted_parameters'
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.0ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (118.1ms)
Run Code Online (Sandbox Code Playgroud)
谁可以帮忙?怎么解决这个问题?
我试图找到一些解决方案,但我真的找不到与运行rails命令时出现的错误相关的任何内容:
rails generate model书名:string summary:text isbn:string
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Running via Spring preloader in process 3579
Expected string default value for '--jbuilder'; got true (boolean)
invoke active_record
identical db/migrate/20170104114702_create_books.rb
identical app/models/book.rb
invoke test_unit
identical test/models/book_test.rb
identical test/fixtures/books.yml
Run Code Online (Sandbox Code Playgroud)
有谁知道可能导致这些错误的原因?
我知道有三个主要的符号为whereActiveRecord方法提供参数:
指定and的where方法是直截了当:
# Pure String notation
Person.where("name = 'Neil' AND age = 27")
# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
Person.where({name: "Neil", age: 27})
Run Code Online (Sandbox Code Playgroud)
or为这个相同的where方法指定是为了哈希语法.可能吗?
# Pure String notation
Person.where("name = 'Neil' OR age = 27")
# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-5
我是铁杆新手.我注意到在rails 5中生成数据迁移时,有些人会使用rails db:migrate它rake db:migrate.有人可以解释数据库迁移中rails vs rake命令之间的区别吗?是否意味着rake命令在rails 5中已经过时了?
非常感谢
我有简单的动作节目
def show
@field = Field.find_by(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
我想要写它的规格
require 'spec_helper'
RSpec.describe FieldsController, type: :controller do
let(:field) { create(:field) }
it 'should show field' do
get :show, id: field
expect(response.status).to eq(200)
end
end
Run Code Online (Sandbox Code Playgroud)
但我有一个错误
Failure/Error: get :show, id: field
ArgumentError:
unknown keyword: id
Run Code Online (Sandbox Code Playgroud)
怎么解决?
ruby-on-rails ×10
ruby-on-rails-5 ×10
ruby ×3
activerecord ×2
autoload ×1
devise ×1
javascript ×1
jquery ×1
rake ×1
rspec ×1
rspec-rails ×1
turbolinks ×1