使用rails generate migration AddClientToUser
创建迁移文件后,我可以像这样编辑迁移文件:
class AddClientToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.references :client
end
end
def self.down
change_table :users do |t|
t.remove :client_id
end
end
end
这是反转迁移中添加的参考列的正确方法吗?
我一直在Rails 2.3中使用Authlogic,但是现在我在使用Rails 3,我想我可以尝试一种新的身份验证解决方案。
Devise与Authlogic相比如何? 他们有什么区别?
Rails 3.0已弃用f.error_messages
,现在需要一个插件才能正常工作-但是,我想学习如何以(新的)本机方式显示错误消息。 我正在遵循入门指南,该指南在实现评论表单时使用了不推荐使用的方法。 例如:
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= f.error_messages %>
<div class="field">
<% f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是正确的方法(由脚手架生成):
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
. . .
我了解在后一个示例中使用了@post
变量,但是在前一个示例中我引用了哪个变量来获取用于创建注释的错误消息?
我在这里使用的是简单表单,但这也是普通的Rails表单的问题。 使用浅层路由时,form_for需要使用不同的参数,具体取决于使用的上下文。
示例:对于编辑(http://localhost:3000/notes/2/edit
),_form.html.erb需要具有simple_form_for(@note)
。但是对于创建新笔记(http://localhost:3000/customers/2/notes/new
)_form.html.erb需要simple_form_for([@customer, @note])
。如果任何一个接收到错误的参数,我都会得到一个找不到的方法 错误。
处理此问题的最佳方法是什么?
这些是我唯一的选择吗?
示例如下:
config / routes.rb
Billing::Application.routes.draw do
resources :customers, :shallow => true do
resources :notes
end
end
耙路| grep注意
customer_notes GET /customers/:customer_id/notes(.:format) notes#index
POST /customers/:customer_id/notes(.:format) notes#create
new_customer_note GET /customers/:customer_id/notes/new(.:format) notes#new
edit_note GET /notes/:id/edit(.:format) notes#edit
note GET /notes/:id(.:format) notes#show
PUT /notes/:id(.:format) notes#update
DELETE /notes/:id(.:format) notes#destroy
app / views / notes / _form.html.erb
# v----------------------------- Right here
<%= simple_form_for (@note), html: { class: 'form-vertical'} do |f| %>
<%= f.input :content %>
<%= f.button :submit %>
<% end -%>
app / views / notes / new.html.erb
<h1>New note</h1>
<%= render 'form' %>
<%= link_to 'Back', customer_path(@customer) %>
app / views / notes / edit.html.erb
<h1>Editing note</h1>
<%= render 'form' %>
<%= link_to 'Show', @note %>
<%= link_to 'Back', customer_path(@customer) %>
app / controllers / notes_controller.rb
class NotesController < ApplicationController
def show
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
respond_to do |format|
format.html
format.json {render json: @note }
end
end
# GET /notes/new
# GET /notes/new.json
def new
@note = Note.new
@customer = Customer.find(params[:customer_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @note }
end
end
# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
end
# POST /notes
# POST /notes.json
def create
@customer = Customer.find(params[:customer_id])
@note = @customer.notes.build(params[:note])
respond_to do |format|
if @note.save
format.html { redirect_to @customer, notice: 'Note was successfully created.' }
format.json { render json: @note, status: :created, location: @note }
else
format.html { render action: "new" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# PUT /notes/1
# PUT /notes/1.json
def update
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to @customer, notice: 'Note was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note = Note.find(params[:id])
@note.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
end
我正在测试Web应用程序的前端,并且想测试在AJAX请求之间的各种转换如何出现以及各种延迟。 有什么方法可以将sleep(1500)
添加到控制器中以延迟响应?
我正在尝试做这样的事情,但是没有用。 我将如何在Rails 3中做到这一点?
Student.find(12).includes(:teacher)
假设我在名为user_ids
的数组中有15个用户ID。
例如,如果我想将其所有名称都更改为“ Bob”,我可以这样做:
users = User.find(user_ids)
users.update_all( :name => 'Bob' )
但是,这不会触发回调。 如果我需要在保存这些记录时触发回调,据我所知,唯一的方法是使用:
users = User.find(user_ids)
users.each do |u|
u.name = 'Bob'
u.save
end
但是,这可能意味着控制器动作中运行时间非常长的任务。
因此,我的问题是,是否还有其他更好/更高性能/更好的方法来触发对记录集的批处理更新,从而触发记录上的回调?
设置如下:
New Rails应用程序,然后将此test_rake.rake放入lib / tasks:
task :testclass do
HelloClass.hello
end`
将hello_class.rb放入app / models或lib /中,并将以下行:config.autoload_paths += %W(#{config.root}/lib)
添加到config.rb
class HelloClass
def self.hello
puts 'hello_class'
end
end
耙测试类给出此错误:
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/Users/name/Sites/Rails/indexapp/lib/tasks/test_class.rake:5:in `block (2 levels) in <top (required)>'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/name/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `load'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `<main>'
有任何想法吗? 我已经通过RVM卸载并重新安装了Ruby,删除/重建了gemset,打印了自动加载路径,以确保hello_class.rb位于其中之一...
我可以从.rake文件中手动要求HelloClass,但是随后我必须对HelloClass所依赖的任何内容执行相同的操作-例如,如果HelloClass包含HTTParty或任务设置了延迟的工作。
任何帮助都是极好的。 谢谢!
我想使用find_or_create_by,但是此语句不起作用。 它不会使用其他属性来“查找”或“创建”。
productproperty = ProductProperty.find_or_create_by_product_id(:product_id => product.id, :property_id => property.id, :value => d[descname])
关于在Rails 3中使用动态查找器的信息似乎很少,甚至没有,将它们“加”在一起给我一个未知的方法错误。
更新:
最初,我无法满足以下要求。 请假设我不是白痴,并且“产品”是产品AR模型的一个实例。
product.product_properties.find_or_create_by_property_id_and_value(:property_id => 1, :value => "X")
错误方法是:
no such keys: property_id, value
我不知道这一点。 直到今天早上,我才找到了传递像这样的值的参考:
product.product_properties.find_or_create_by_property_id_and_value(1, "X")
而且,效果很好。 我本来希望哈希可以在相同的情况下工作,但我猜不是。
因此,如果您错过互联网上的某些内容,我想您会被否决吗?
我正在尝试创建一个基于Ajax的评论表单,该表单将在提交时更新我的评论列表。 很基本的东西。
我有一个部分comments/_foobar.html.haml
,其中有一个带有基本评论信息的foobar
标签,这是我的render
文件(实际上,这是三个不同的测试文件,合并为一个文件,向您展示我的问题是什么):
$('#comments ul.comments').append("<%= render :partial => 'comments/single', :locals => { :c => @comment } %>");
$('#comments ul.comments').append("<%= render :partial => 'comments/foobar' %>");
$('#comments ul.comments').append("foobar");
alert('foobar');
comments/_foobar.html.haml
的内容只是foobar
,内部没有html。我的问题是前两行不起作用。 我的开发服务器控制台中没有错误,对象检查器说注释代码已正确返回,但未添加到我的注释列表中。 第三行工作正常,因此第四行。 使用render
似乎有些问题。
这个问题已经在这里有了答案:
我正在使用Ruby on Rails 3.0.7,并且打算使用部分模板。 我的应用程序中的所有类都将使用相同的局部函数,因此我必须决定将所有这些类放在何处。
将“全局”共享部分模板放在lib
文件夹中是一个好主意吗? 如果没有,选择放置这些文件夹的常用方法是什么? 关于如何正确命名和加载该文件夹的任何建议?
find
、where
和find_by_id
有什么区别? 当您尝试查找具有ID的用户时,它们都起作用。
我希望只在Gemfile
环境中运行我的一个初始化程序(在:development
中)中的代码,而不在config/initializers/barista_config.rb
或Barista.configure
环境中运行。最佳方法是什么?
将其粘贴到:development
中似乎是不干净的,而且我不太喜欢将整个初始化程序文件包装在Gemfile
语句中。 有一些规范的方法可以做到这一点吗?
(背景:为了加快测试加载时间,我尝试将Barista gem移到Gemfile
的:development
组中,但是config/initializers/barista_config.rb
调用Barista.configure
,所以现在它在测试(和生产)模式下很麻烦。)
给出:message = Mail.new(params[:message])
如此处所示:[http://docs.heroku.com/cloudmailin]
它显示了如何将message.body作为HTML获取,如何获取纯文本/文本版本?
谢谢
我想知道为什么有人应该在控制器内使用helper_method来创建一个辅助方法,而不是创建位于辅助文件内的“常规”方式。 那有什么利弊?
我想知道您在使用ElasticSearch和Tire时如何在应用程序中测试搜索。
如何设置新的ElasticSearch测试实例? 有没有办法模拟它?
您知道的任何宝石都可以帮助您吗?
我发现一些有用的东西:
我找到了一篇很棒的文章,几乎回答了我所有的问题:)
[HTTP://bits and bit.饿死/post/11295134047/unit-testing-with-tire-安定-elastic-search#第三曲是_thread]
另外,Tire作者Karmi还提供了答案。
这也很有用:[https://github.com/karmi/tire/wiki/Integration-Testing-Rails-Models-with-Tire]
我不敢相信我在问之前没有找到这些...
我读了很少的有关NaN
的帖子,但没有弄清楚如何在Ruby on Rails中处理它。 我想检查一个值是否是NaN
,我想用零(0)替换它。我尝试了以下
logger.info(".is_a? Fixnum #{percent.is_a? Fixnum}")
当百分比为NaN
时,返回我为假。
我在记录器中所做的更改很少
logger.info("Fixnum #{percent.is_a? Fixnum} percent #{percent}")
输出量
Fixnum false percent 94.44444444444444
Fixnum false percent NaN
Fixnum false percent 87.0
我有一个表“用户”,列为“电子邮件”。 它曾经是唯一的(带有索引),但是一个新的要求是允许使用nil。
有没有比以下更好的解决方案:
remove_index :users, :email
add_index :users, :email
?
最初,它是通过唯一选项添加的:
add_index :users, :email, :unique => true
我认为通知将是它自己的资源,并且与带有表示关联的联接表的用户模型具有has_many, through
关系。
具有许多通知的用户是显而易见的,然后一个通知将具有许多用户,因为将存在与许多用户相关联的许多标准化通知(评论通知,后续通知等)。
除了此设置之外,我不确定如何根据您的应用程序中的某些事件触发创建通知。 我也不太确定如何设置路由-是它自己的单独资源还是嵌套在用户资源中? 如果有人可以对此进行扩展,我会发现它很有帮助。
最后,ajax轮询可能会改善这种功能。
我可能缺少某些内容,因此请填写此内容,以便它成为很好的常规资源。
我当前在网站的每个页面的标题栏中都有一个登录弹出窗口。 我希望能够在成功登录后重新加载此人所在的当前页面。 如何在控制器中执行此操作?
def create
#declaring and defining user variable stuff
if user.save
#reload current page <--how do I do this?
end
end
谢谢