小编Ben*_*ret的帖子

如何在ROR(Ruby)中显示PDF?

我在互联网上环顾四周,但似乎无法找到如何在rails中显示PDF(我只能找到有关如何创建PDF的信息).

有谁知道我需要显示哪个代码/ gem?

ruby pdf ruby-on-rails

34
推荐指数
2
解决办法
2万
查看次数

红宝石的"延伸"成本有多高?

首先,我知道怎么extendinclude他们的工作,什么通常用于等等.无论是一个好主意与否是不是我的问题的一部分.

我的问题是:有多贵extend?这是扩展实例和单例对象的常用Javascript技术.人们可以在Ruby中做类似的事情,但是如果在很多对象上使用它会很慢吗?

ruby

19
推荐指数
2
解决办法
1703
查看次数

rails sqlite适配器错误

我正在按照rails教程中的说明操作,并在尝试使用scaffold命令时遇到困难.

运行时:

rails generate scaffold User name:string email:string  
Run Code Online (Sandbox Code Playgroud)

我收到错误:

C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `rescue in establish_connection': Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (can't activate sqlite3 (~> 1.3.4), already activated sqlite3-1.3.3-x86-mingw32. Make sure all dependencies are added to Gemfile.) (RuntimeError)
Run Code Online (Sandbox Code Playgroud)

运行:

gem install activerecord-sqlite3-adapter
Run Code Online (Sandbox Code Playgroud)

我收到了错误:

ERROR:  Could not find a valid gem 'activerecord-sqlite3-adapter' (>= 0) in any repository
ERROR:  Possible alternatives: activerecord-jdbcsqlite3-adapter, activerecord-sqlserver-adapter, activerecord-nulldb-adapter, activerecord-spatialite-adapter, activerecord-simpledb-adapter
Run Code Online (Sandbox Code Playgroud)

我的Gemfile看起来像这样:

source 'http://rubygems.org'
gem 'rails', '3.1.0'  
gem 'sqlite3', '1.3.3'  
...
Run Code Online (Sandbox Code Playgroud)

我在Windows 7 x64操作系统上运行.

有任何想法吗?

ruby sqlite ruby-on-rails

18
推荐指数
2
解决办法
3万
查看次数

如何在Rails中将参数传递给委托方法

我想有一个仪表板来显示多个模型的摘要,我使用Presenter实现它而没有自己的数据.我使用ActiveModel类(没有数据表):

class Dashboard
  attr_accessor :user_id
  def initialize(id)
    self.user_id = id
  end

  delegate :username, :password, :to => :user 
  delegate :address,  :to => :account
  delegate :friends,   :to => :friendship

end 
Run Code Online (Sandbox Code Playgroud)

通过代表,我希望能够打电话Dashboard.address回来Account.find_by_user_id(Dashboard.user_id).address.

如果Dashboard是一个ActiveRecord类,那么我可以声明Dashboard#belongs_to :account并且委托会自动工作(即,Account会知道它应该从Dashboard实例中的user_idequals 返回地址属性to user_id).

但Dashboard不是ActiveRecord类,所以我不能声明belongs_to.我需要另一种方法来告诉Account查找正确的记录.

有办法克服这个问题吗?(我知道我可以假装Dashboard有一个空表,或者我可以将User的实例方法重写为带参数的类方法.但这些解决方案都是黑客攻击).

谢谢.

model-view-controller ruby-on-rails presenter activemodel

13
推荐指数
1
解决办法
5530
查看次数

Html在Rails 3视图中转义

我正在使用Rails 3.我想在erb模板中显示生成的html片段

<%= "<div>Foo Bar</div>" %>
Run Code Online (Sandbox Code Playgroud)

Rails编码div标签.

如果我在Rails 2中正确<%=h导致html转义.似乎它在Rails 3中被更改了.如何在Rails 3中插入没有编码的html片段?

问候,阿列克谢.

xss ruby-on-rails erb ruby-on-rails-3

11
推荐指数
1
解决办法
5599
查看次数

运行"rails s"时遇到问题

rails s在ubuntu中运行时遇到问题.当我键入rails s它不启动服务器,但它给我这个消息:

kyala@ubuntu:~/depot$ rails s

Usage:
  rails new APP_PATH [options]

Options:

-r, [--ruby=PATH]           # Path to the Ruby binary of your choice
                              # Default: /home/kyala/.rvm/rubies/ruby-1.9.2-p290/bin/ruby

-d, [--database=DATABASE]   # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db)enter code here
                              # Default: sqlite3

 -b, [--builder=BUILDER]     # Path to an application builder (can be a filesystem path or URL)

-m, [--template=TEMPLATE]   # Path to an application template (can be a filesystem path or URL)
      [--dev]                 # Setup the application with …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

9
推荐指数
3
解决办法
1万
查看次数

rake预编译资产在rake资产时失败:预编译 - 基本上是空的application.js

运行:

bundle exec rake assets:precompile RAILS_ENV=production --trace

** Execute assets:precompile:primary
rake aborted!
TypeError: Object doesn't support this property or method
  (in C:/Sites/MyApp/app/assets/javascripts/application.js)
Run Code Online (Sandbox Code Playgroud)

以下是整个内容application.js:

//= require jquery
//= require jquery_ujs
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

没有别的.

我试图删除 //= require三行application.js,然后预编译运行没有问题.

ruby-on-rails precompiled

9
推荐指数
1
解决办法
3610
查看次数

Rails 3嵌套资源简称?

我正在将Rails 2.3应用程序升级到Rails 3.在Rails 2.3路由器中,可以:name_prefix在嵌套资源上设置nil以获得更短的名称.实际的URL仍然是完全限定的,但代码可以使用较短的名称.例如,:

 map.resources :sites do |site|
    site.resources :groups, :as => :groups, :controller => :url_groups, :name_prefix => nil, :member => { :clone => :post } do |group|
      group.resources :tests, :as => :tests, :controller => :test_runs, :name_prefix => nil, :collection => { :latest => :get }
    end
  end

允许一个人使用latest_tests_path.我无法弄清楚如何使用Rails 3做同样的事情,所以我坚持不懈latest_site_group_tests_path.如果这是它需要的方式,我可以通过代码并更改它的每个实例.但我想确保我先没有遗漏任何东西.无论好坏,我确实需要保持URL结构,所以浅路径似乎不是答案.

routes helpers ruby-on-rails-3

7
推荐指数
1
解决办法
3884
查看次数

如何在Rails 3中完成这项工作?

我正在更新一个Rails项目,发现一段代码在Rails 3和Ruby 1.9.2中没有用(它在Rails 2.3和Ruby 1.8.7中都有用),反正看起来很乱!

  def speech_box(title, options = {}, &block)
    block_to_partial 'layouts/speech_box', options.merge(:title => title), &block
  end

  def block_to_partial(partial_name, options = {}, &block)
    options.merge!(:body => capture(&block))
    concat(render(:partial => partial_name, :locals => options), block.binding)
  end
Run Code Online (Sandbox Code Playgroud)

我打电话时收到以下错误:

  <% speech_box("hello") do %>
    <p>lorem ipsum</p>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

错误的参数数量(2为1)

为什么这在Rails 3中不起作用?

如下所述,这里是跟踪:

actionpack (3.1.0) lib/action_view/helpers/text_helper.rb:51:in `concat'
app/helpers/application_helper.rb:277:in `block_to_partial'
app/helpers/application_helper.rb:272:in `speech_box'
app/views/talks/new.html.erb:46:in `_app_views_talks_new_html_erb__3484685084088947386_2193902320'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render' activesupport (3.1.0)
lib/active_support/notifications.rb:55:in `instrument' actionpack (3.1.0)
lib/action_view/template.rb:142:in `render' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails-3

6
推荐指数
1
解决办法
922
查看次数

使用rails Mail_form gem时出错:"未初始化的常量Mailform"

所以,我已经在stackoverflow和其他地方检查了类似问题的所有现有答案,但是无法让mail_form gem像宣传的那样工作.

这是设置:我正在尝试为我公司的网站创建一个简单的潜在客户捕获表单.我希望将表单收集的数据通过电子邮件发送到我的电子邮件帐户而不需要数据库后端,我认为这就是mail_form会变得简单.

这是我的模型,ContactForm.rb:

class ContactForm < Mailform::Base
  attribute :name,     :validate => true
  attribute :email,    :validate => /\A([\w\.%\+\-]+)@([\w]{2,})\z/i
  attribute :file,     :attachment => true
  attribute :phone 
  attribute :referral
  attribute :message
  attribute :nickname, :captcha => true

  def persisted?
    false
  end

  def headers
    {
      :subject => "New Lead",
      :to => "jonthewineguy@gmail.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的控制器,contact_forms_controller.rb:

class ContactFormsController < ApplicationController
  def new
    @contact_form = ContactForm.new
  end

  def create
    begin
      @contact_form = ContactForm.new(params[:contact_form])
      @contact_form.request = request
      if …
Run Code Online (Sandbox Code Playgroud)

notifications ruby-on-rails html-email form-for

5
推荐指数
2
解决办法
2397
查看次数