主要目标:允许插件,宝石向预定义表单添加其他表单字段.
例如,应用程序有一个设计登录表单:
所有注册表单中添加一个附加字段.<%= form_for(resource, :as => resource_name, ...) do |f| %>
<%= devise_error_messages! %>
...
<% end %>
有没有办法从我的rails-plugin/railtie向表单添加一个额外的字段,并定义一个promo code回调方法(对我的附加字段数据采取行动)?
优点:
看看ActionView代码,似乎没有内置的方法.你的想法是什么?
注意:Drupal的on_submit钩子就是一个很好的例子.
我已将我的应用程序从rails 2.3迁移到rails3,我的回形针有问题.我看到在paperclip git上有一个rails3的分支.
所以我在Gemfile中添加了"gem'paperclip',:git =>'git://github.com/thoughtbot/paperclip.git',: branch =>'rails3'"并启动命令bundle install.
安装回形针后,上传工作正常,但不是样式.我看到了修复它的黑客攻击.
# in lib/paperclip/attachment.rb at line 293
def callback which #:nodoc:
# replace this line...
# instance.run_callbacks(which, @queued_for_write){|result,obj| result == false }
# with this:
instance.run_callbacks(which, @queued_for_write)
end
Run Code Online (Sandbox Code Playgroud)
之后风格还可以,但我无法激活处理器.我的代码是:
has_attached_file :image,
:default_url => "/images/nopicture.jpg",
:styles => { :large => "800x600>",
:cropped => Proc.new { |instance| "#{instance.width}x#{instance.height}>" },
:crop => "300x300>" },
:processors => [:cropper]
Run Code Online (Sandbox Code Playgroud)
我的处理器位于RAILS_APP/lib/paperclip_processors/cropper.rb中,包含:
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command and !skip_crop?
crop_command + super.sub(/ -crop \S+/, …Run Code Online (Sandbox Code Playgroud) 我尝试使用推荐的方式(来自Rails指南)来测试插件中生成的路由,但测试仍然失败.
奇怪的是,如果我在创建路线后重新加载路线(或者我认为),测试失败,但如果我让测试经过一次(例如使用自动测试),那么路线会在后续尝试中被识别.
这是代码:
describe "named route report_with_last_name_smith_path" do
before :all do
Reports::Application.routes.draw do
match "/report_some_report_for_us" => "report#report_some_report_for_us",
:as => :report_some_report_for_us
end
Rails.application.reload_routes! # If I leave this out, then the test
# passes the second time that autotest/autospec
# go through.
end
it "route for every record" do
{:get => '/report_some_report_for_us'}.should route_to(:controller => 'report', :action => 'report_some_report_for_us')
end
end
Run Code Online (Sandbox Code Playgroud)
知道如何让它一直通过吗?
我最近将Devise身份验证系统集成到rails测试应用程序中.测试应用程序只包含一个位于身份验证后面的项目模型/控制器/视图.
我现在正在添加测试版邀请系统,以便只有收到其他用户邀请的用户才能加入该网站.我通过以下方式实现了这个系统:http://railscasts.com/episodes/124-beta-invitations.
我遇到的一个问题是beta邀请函要求我向用户控制器添加一些逻辑,这是你无法通过Devise完成的.我正在尝试使用Users :: RegistrationsController <Devise :: RegistrationsController创建一个新的注册控制器,它基本上与Devise控制器相同,但允许我为beta邀请系统添加一些额外的逻辑.
但是,我不能让这个新的控制器工作(我也在这个新控制器中应该包含的内容方面遇到麻烦).我在路线文件中添加了以下内容:
资源:注册
资源:邀请
资源:项目
devise_for:用户
devise_scope:用户确实
得到'users/sign_up /:invitation_token'=>'registrations #new'
结束
我在这个新的注册控制器中添加了什么来模仿原始设计/注册控制器的功能?
我正在尝试为插件"foobar"开发测试,修改一些标准的Rails助手.在vendor/plugins/foobar/test/foobar_test.rb中,我有以下内容:
# create the test model
class Thing < ActiveRecord::Base
end
# create the test controller, which renders the included index template
class ThingsController < ActionController::Base
def index
@things = Thing.all
format.html { render(:file => 'index') }
end
def destroy
@thing = Thing.find(params[:id])
@thing.destroy
format.html { render(:file => 'index') }
end
end
# confirm that the test environment is working correctly
class ThingsTest < ActiveSupport::TestCase
test "model is loaded correctly" do
assert_kind_of Thing, Thing.new
end
end
# confirm that the …Run Code Online (Sandbox Code Playgroud) 我在rails项目中使用了serialize_with_options(http://www.viget.com/extend/simple-apis-using-serializewithoptions/),并且已根据链接页面上的示例使用命名块进行渲染:
class Speaker < ActiveRecord::Base
# ...
serialize_with_options do
methods :average_rating, :avatar_url
except :email, :claim_code
includes :talks
end
serialize_with_options :with_email do
methods :average_rating, :avatar_url
except :claim_code
includes :talks
end
end
Run Code Online (Sandbox Code Playgroud)
然后我可以使用@ speaker.to_xml(:with_email)调用第二个块配置.这很好用,但是,当我有一个对象数组时,我想弄清楚如何调用这个块.例如,以下内容不起作用:
@speakers = Speaker.all
@speakers.to_xml(:with_email)
Run Code Online (Sandbox Code Playgroud)
返回"TypeError:can not dup Symbol"错误.这对我来说很有意义,因为Array尚未配置为使用serialize_with_options.如何在运行.to_xml并渲染所有发言者时将此标记传递给各个发言人对象:with_email?
我正在尝试使用devise的reset_password_token强制用户在第一次登录时更改密码,使用我在此处阅读的内容:Rails设计:设置密码重置令牌并重定向用户
我得到的页面加载显示"设置您的密码",但每次我点击提交时,都会发出一条通知,说明reset_password_token已过期并请求新的.我已经尝试将reset_password_token设置为变量,而不是通过用户对象(@ user.reset_password_token)进行访问,以防万一是通过访问该令牌计算到期,但是当我尝试设置初始密码时它仍然表示它已过期.我不确定如何计算到期时间,有人有想法吗?
我的Rails 3.1应用程序中有以下spec_helper.rb文件.我正在使用Spork加载环境以便进行测试.在将Spork加入混合物之前,我的所有测试都有效.添加spork之后,测试数据库在测试运行之间没有得到正确的清除,这导致了我的一些期望.
按照其他说明,我将database_cleaner添加到下面列出的代码中; 但是,现在,开发数据库以及测试数据库正在被清理.ENV ["RAILS_ENV"]呼叫在此呼叫期间返回测试.
有没有办法显式限制对DatabaseCleaner.clean_with(:truncation)的调用只影响测试数据库?
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers/integrations/rspec'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :mocha
config.formatter = 'documentation'
config.use_transactional_fixtures = true
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
FactoryGirl.reload
end
Run Code Online (Sandbox Code Playgroud)
更新:这是我的database.yml文件
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: …Run Code Online (Sandbox Code Playgroud) 我有这个错误,这使得无法加载我的应用程序
no such file to load -- coffee_script
(in /Users/damien/projects/easyJobs/app/assets/javascripts/jobs.js.coffee)
Extracted source (around line #10):
7: /[if lt IE 8]
8: = stylesheet_link_tag 'ie.css', :media => 'screen, projection'
9: = stylesheet_link_tag :application
10: = javascript_include_tag :application
11: = csrf_meta_tag
12:
13: %body
Run Code Online (Sandbox Code Playgroud)
这些文件都在那里,但看起来不像是看到它们的原因超出了我:/如果有人知道为什么会发生这个错误让我知道会非常感激.
达米安
ruby on rails上是否有任何gem/plugin,它能够在运行时在模型中定义自定义字段,而无需为每个不同的字段更改模型本身.
我正在寻找像Redmine acts_as_customizable插件这样的东西,它被封装为可用于rails方式的gem,即
gem 'gemname'
rails g something
rails db:migrate
class Model < ActiveRecord::Base
acts_as_something
end
Run Code Online (Sandbox Code Playgroud)
以下是Redmine中使用的CustomField和CustomValue类.
由于我的问题不明确,我添加了一个简短的用例,更好地解释了我的需求:
我希望用户能够设计自己的表单,并收集在这些表单上提交的数据.一个重要的决定是如何存储和访问这些自定义动态记录的设计.
从这里开始,在本文中用不同的想法来解决问题,但它们都有缺点.出于这个原因,我问是否已经在一些宝石中找到了问题而无需重新考虑整个问题.
activerecord ruby-on-rails dynamic-data ruby-on-rails-plugins
devise ×2
ruby ×2
activerecord ×1
coffeescript ×1
dynamic-data ×1
paperclip ×1
rspec ×1
spork ×1