我想用rspec测试控制器的动作和flash消息的存在.
行动:
def create
user = Users::User.find_by_email(params[:email])
if user
user.send_reset_password_instructions
flash[:success] = "Reset password instructions have been sent to #{user.email}."
else
flash[:alert] = "Can't find user with this email: #{params[:email]}"
end
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
规格:
describe "#create" do
it "sends reset password instructions if user exists" do
post :create, email: "email@example.com"
expect(response).to redirect_to(root_path)
expect(flash[:success]).to be_present
end
...
Run Code Online (Sandbox Code Playgroud)
但我有一个错误:
Failure/Error: expect(flash[:success]).to be_present
expected `nil.present?` to return true, got false
Run Code Online (Sandbox Code Playgroud) 我想在ActiveAdmin控制器中添加before_action过滤器.
我可以这样做:
before_action :set_product, only: [:show, :edit, :update, :destroy]
private
def set_product
@product = Product.find_by_name(params[:name])
end
Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails ruby-on-rails-3 activeadmin ruby-on-rails-4
我的项目在当地运作良好.我把它部署到了Heroku.我有一些错误,应用无法在那里运行.我正在使用带有postgresql的rails 4.无法理解为什么会这样.在我看来,没有创建数据库.我应该在database.yml上添加指定生产设置吗?
database.yml的
development:
adapter: postgresql
encoding: utf8
database: test_store_dev
pool: 5
username: mike
password: mike
host: 127.0.0.1
test:
adapter: postgresql
encoding: utf8
database: test_store_test
pool: 5
username: mike
password: mike
host: 127.0.0.1
Run Code Online (Sandbox Code Playgroud)
从日志:
~/workspace/test_store$ heroku logs --tail
2014-02-13T13:14:47.731382+00:00 app[web.1]: 3:
2014-02-13T13:14:47.731535+00:00 app[web.1]: 6: <% Category.roots.each do |category| %>
2014-02-13T13:14:47.731382+00:00 app[web.1]: : SELECT "categories".* FROM "categories" WHERE "categories"."parent_id" IS NULL ORDER BY "categories"."lft"):
2014-02-13T13:14:47.731972+00:00 app[web.1]: I, [2014-02-13T13:13:06.802166 #2] INFO -- : Started GET "/" for 46.53.195.24 at 2014-02-13 13:13:06 +0000 …Run Code Online (Sandbox Code Playgroud) 在我的数据库中,有很多用户使用无效数据(没有手机号码).因为我刚刚添加了移动存在验证.当我尝试添加新事件时,我收到错误消息"用户移动电话号码必须是...格式".如何在创建事件时跳过用户验证?
event.rb
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)
user.rb
class User < ActiveRecord::Base
has_many :participations
has_many :events, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)
events_controller.rb
def create
@event = Event.new(event_params)
respond_to do |format|
if @event.save
format.html { redirect_to events_path, notice: 'Event was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @event, :html => {:class => 'row'} do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用地理编码器获取当前坐标.
但
request.location.latitude
request.location.longitude
Run Code Online (Sandbox Code Playgroud)
总是为我返回0.0和0.0.是否可以使用地理编码器找到地理定位坐标?
提前致谢!