我正在使用设计,rolify和cancan.我也在使用rspec和FactoryGirl进行测试.现在我正在进行一些测试,我想为这些测试定义具有不同角色的用户.以下是我目前对如何使用FactoryGirl进行测试的猜测:
FactoryGirl.define do
factory :user do
name 'Test User'
email 'example@example.com'
password 'please'
password_confirmation 'please'
# required if the Devise Confirmable module is used
confirmed_at Time.now
factory :admin do
self.has_role :admin
end
factory :curator do
self.has_role :curator
end
factory :super_admin do
self.has_role :super_admin
end
end
end
Run Code Online (Sandbox Code Playgroud)
以下是我的一些测试似乎没有正确编写:require'pec_helper'
describe "Pages" do
subject { page }
before do
@newpage = FactoryGirl.create(:page)
@newpage.save
end
context 'not logged in' do
it_behaves_like 'not admin'
end
context 'logged in' do
context 'as user' do
before do …Run Code Online (Sandbox Code Playgroud) 使用rails,devise,rspec和factorygirl:
试图为我的网站创建一些测试.我正在使用确认模型进行设计,因此当我使用FactoryGirl创建用户时,用户未得到确认.
这是我的工厂.rb:
FactoryGirl.define do
factory :user do
full_name "Aren Admin"
email "aren@example.com"
password "arenaren"
password_confirmation "arenaren"
role_id ADMIN
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的rspec测试文件:
require 'spec_helper'
describe "Admin pages" do
subject { page }
describe "home page" do
let(:user) { FactoryGirl.create(:user) }
before { visit admin_home_path }
it { should have_content("#{ROLE_TYPES[user.role_id]}") }
end
end
Run Code Online (Sandbox Code Playgroud)
我收到错误,因为用户未得到确认.通过搜索我很确定我需要使用'确认'方法!并且它属于factories.rb文件,但我不知道在哪里放它.
我在ActiveAdmin中为这些模型设置了一个嵌套表单(a:class_section has_many:class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
和
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Run Code Online (Sandbox Code Playgroud)
当我看到表格时,一切似乎都在正确的位置.但是,当我更新class_date时,它不会保存.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: …Run Code Online (Sandbox Code Playgroud) 在Heroku上执行此操作的最佳方法是什么?通过控制台?我确实为所有依赖表设置了'dependent :: destroy'.
我想删除字符串中的非字母数字字符,但不删除国际字符,如重音字母.我也想保留空白.这是我到目前为止:
the_string = the_string.gsub(/[^a-z0-9 -]/i, '')
Run Code Online (Sandbox Code Playgroud)
这确实删除了国际重音字母字符.
我使用的解决方案:
the_string = the_string.gsub(/[^\p{Alnum}\p{Space}-]/u, '')
Run Code Online (Sandbox Code Playgroud)
有用!谢谢.
我有两个模特用于表演和表演(在戏剧或喜剧节目中表演).它们在模型中如下关联:
class Show < ActiveRecord::Base
has_many :performances, :dependent => :destroy
accepts_nested_attributes_for :performances
end
class Performance < ActiveRecord::Base
belongs_to :show
end
Run Code Online (Sandbox Code Playgroud)
在Performance模型中有一个名为start_time的日期时间.
如何在模型中定义范围,该范围返回至少具有以下性能的所有节目:start_time是将来的?
另外,如何定义一个范围,该范围返回所有没有任何性能的节目:start_time是将来的?
鉴于我将所有需要的信息存储在rails应用程序中的:event模型中.如何向视图添加按钮,单击该按钮时会提供动态创建的ics/ics文件?
我环顾了几天,我发现的所有信息似乎已经过时或假设我已经知道如何做大块的解决方案了.
设计是否容易受到正常http://连接的会话劫持的影响?我无法从文档中找出它.
允许人们将图像上传到云服务器(在Rails 3应用程序中)的最简单方法是什么?我想在表单中包含一个"上传图片"按钮,并将地址存储在我的模型中.我也希望能够限制文件大小.
我有场地和照片的模型:
class Venue < ActiveRecord::Base
has_and_belongs_to_many :photos
validates :name, presence: true
validates :permalink, presence: true, uniqueness: true
def to_param
permalink
end
end
class Photo < ActiveRecord::Base
mount_uploader :image, PhotoUploader
has_and_belongs_to_many :venues
end
Run Code Online (Sandbox Code Playgroud)
我正在使用simple_form来制作以下表格:
<div class="content-box">
<%= simple_form_for [:admin, @venue] do |f| %>
<%= f.input :name %>
<%= f.input :permalink %>
<%= f.input :description, input_html: { cols: 100, rows: 6 }%>
<%= f.input :address %>
<%= f.input :city %>
<%= f.input :phone %>
<%= f.association :photos %>
<%= f.button :submit, class: …Run Code Online (Sandbox Code Playgroud) 我在使用Open Office时遇到问题.我正在尝试将一些文件导出为CSV文本,并且看起来下划线以及可能正在替换其他一些字符.例如,我在文本编辑器中创建了此文件并将其保存为test.csv:
"this_value","is_replaced"
Run Code Online (Sandbox Code Playgroud)
然后我将文件作为电子表格打开到Open Office.看起来很好.然后我将其保存为名为test-export.csv的新CSV文件.当我在文本编辑器中打开新文件时,下划线将替换为"+ AF8-",逗号将替换为选项卡.
this+AF8-value is+AF8-replaced
Run Code Online (Sandbox Code Playgroud)
此更改使文件对我的目的无用.知道是什么导致了这个吗?
我正在使用rails教程中的一个帮助器方法,它将两个字符串连接在一起,以便在视图中的标题选择器中使用.它工作得非常好,除非字符串中有撇号.
当:group_name包含一个撇号时,它会像这样出现:
<title>The website | O&#x27;Malley Brothers</title>
Run Code Online (Sandbox Code Playgroud)
这是方法:app/helpers/application_helper.rb
module ApplicationHelper
def full_title(page_title)
base_title = "Chicago Improv Festival"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
Run Code Online (Sandbox Code Playgroud)
以下是它在布局视图中的使用方式.应用程序/视图/布局/ application.html.erb:
<title><%= full_title(yield(:title)) %></title>
Run Code Online (Sandbox Code Playgroud)
以下是:标题在另一个视图文件中设置的位置:app/views/submissions/show.html.erb
<% provide(:title, @submission.group_name) %>
Run Code Online (Sandbox Code Playgroud) associations ×2
devise ×2
factory-bot ×2
rspec ×2
activeadmin ×1
cloud ×1
console ×1
csv ×1
forms ×1
formtastic ×1
has-many ×1
heroku ×1
icalendar ×1
image ×1
regex ×1
ruby ×1
scope ×1
simple-form ×1
string ×1