小编yel*_*ign的帖子

Rails"没有这样的文件加载 - ruby​​gems(LoadError)"

我刚刚从Heroku切换到亚马逊网络服务.以前我使用带有Ruby的命令提示符(来自我的Windows PC)运行了所有Rails命令行命令.但是,在我登录到我的Amazon Linux EC2实例,然后进入我的应用程序目录后,我收到此错误:

/usr/bin/rails:9:in `require': no such file to load -- rubygems (LoadError)
    from /usr/bin/rails:9
Run Code Online (Sandbox Code Playgroud)

当我尝试运行任何命令时rails console.

我回顾了这个问题,但似乎并没有多个Ruby库适用于我,因为

which -a ruby

只产生一个位置:

/usr/bin/ruby

此外,这个问题似乎没有帮助,因为我没有.当我尝试输入时

rvm use 1.9.3

我收到此消息:

-bash: rvm: command not found

这是我第一次看到Linux环境,所以任何帮助都会受到赞赏.谢谢!

linux ruby-on-rails amazon-ec2 amazon-web-services

5
推荐指数
1
解决办法
5295
查看次数

如何在Google Analytics中跟踪设计用户注册为转换

我正在使用Devise和我的Rails 3.2应用程序,我希望能够在Google Analytics中添加跟踪新注册作为转换.我想让新用户定向到他们被重定向到现在的同一页面,如果可能的话(也就是说,可能是通过重定向到当前页面的用户被重定向到创建后的视图).

有人可以帮我找出使用Devise做到这一点的最佳方法吗?

# users/registrations_controller.rb
# POST /resource
def create
  build_resource
  if resource.save        
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_navigational_format?
      sign_up(resource_name, resource)
      respond_with resource, :location => after_sign_up_path_for(resource)
    else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
      expire_session_data_after_sign_in!
      respond_with resource, :location => after_inactive_sign_up_path_for(resource)
    end
  else
    clean_up_passwords resource
    respond_with resource
  end
end

def after_sign_up_path_for(resource)
  after_sign_in_path_for(resource)
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise

5
推荐指数
1
解决办法
1894
查看次数

在Heroku上更改为MySQL2时出错

对于我的Heroku应用程序(Rails 3.1.4和Ruby 1.9.2),我正在尝试更改为使用MySQL2的数据库,但是我收到来自Heroku的错误(这会导致我的应用程序崩溃):

3.1.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `rescue in establish_connection': Please install the mysql12 adapter: `gem install activerecord-mysql12-adapter` (no such file to load -- active_record/connection_adapters/mysql12_adapter) (RuntimeError) EXCEPT
Run Code Online (Sandbox Code Playgroud)

在我的gemfile中,我有:

group :production do
  gem "mysql2", "~> 0.3.11"
end

group :development, :test do
  gem 'mysql', '2.8.1'
end
Run Code Online (Sandbox Code Playgroud)

在我的database.yml中,我有:

development:
  adapter: mysql

production:
  adapter: mysql2
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的失败(所有尝试都在本地和Heroku中正确安装):

  1. 根据这个答案,我试过(在我的gemfile中),mysql2版本"<0.3"

  2. 对于那个问题的另一个答案,我尝试了'<0.3.7'但没有用

  3. 我按照这个答案尝试了宝石"mysql2","〜> 0.3.11" ,但它没有用

  4. 根据gem的网站,我尝试(在我的gemfile中),mysql2版本"〜> 0.2.7"并安装了mysql2 0.2.18(在本地成功并在Heroku中)

heroku mysql2 ruby-on-rails-3

4
推荐指数
1
解决办法
2212
查看次数

使用简单表单格式化日期

我在我的Rails应用程序中使用Simple Form gem.在我的注册表格中,我有一个出生日期字段.但是,字段按以下顺序显示:年/月/日.

什么是获得现场订单的最佳方式月 - 日 - 年?

我的表单看起来像这样:

 <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
   <%= f.input :created_at %>          
   <%= f.button :submit, "Sign up" %>      
<% end %>
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails-3 simple-form

4
推荐指数
2
解决办法
2067
查看次数

每当没有Capistrano的宝石时,我可以使用Rails吗?

我有一个我在亚马逊的Elastic Beanstalk上托管的Rails应用程序.我想使用Whenever gem来安排任务,但Whenever gem文档和这个Railscast都提到了与Capistrano的集成.我没有使用Capistrano来管理我的服务器,所以我不确定它是否会弄乱我的服务器现在如何操作,如果我安装它只是为了使用Whenever.

也许另一种问我问题的方法是在Capistrano的deploy.rb文件中包含这个命令是做什么的,如果我不使用Capistrano就可以替代这样做:

set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
Run Code Online (Sandbox Code Playgroud)

capistrano ruby-on-rails whenever

4
推荐指数
1
解决办法
983
查看次数

在 Rails 中的文本字段中显示链接

在我的 Rails 3.1 应用程序中,我有一个用于评论的文本字段,我希望能够允许人们包含可点击的链接(而不仅仅是显示为纯文本的 url),以及让文本字段识别用户何时有文本字段中的换行符(无需用户添加 html)。我怎样才能做到这一点?

如果用户将 html 放在 href 中,这适用于显示链接: <%= simple_format(@user.description) %>

这适用于识别和显示 text_field 中回车符的换行符: <%= h(@user.description).gsub(/\n/, '<br/>').html_safe %>

但是,我还没有想出如何一起做。

ruby-on-rails-3

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

在 Rails 中使用 gsub 将字符串替换为符号

我有一个 Rails 应用程序,它使用 simple_feed gem 读取 RSS 提要。然而,某些提要无法正确读取 - 即某些标题具有:

`&#x2018;`
Run Code Online (Sandbox Code Playgroud)

或者

`&#x2019;`
Run Code Online (Sandbox Code Playgroud)

代替: ”

我的标题读作:

i.title
Run Code Online (Sandbox Code Playgroud)

我以为 gsub 可以轻松解决这个问题,但我很难让它发挥作用。我试过:

i.title.gsub(%r[&#x2018;]/, '"')
Run Code Online (Sandbox Code Playgroud)

我什至不确定这是否有效,但它注释掉了#.

然后我尝试:

i.title.gsub(%r["&#x2018;"]/, '*')
Run Code Online (Sandbox Code Playgroud)

结果是:

C:/Sites/foo/app/views/bar/show.html.erb:20: syntax error, unexpected ','
...( i.title.gsub(%r["&#x2018;"]/, '*') )
Run Code Online (Sandbox Code Playgroud)

我以前没有真正使用过 gsub,我正在尝试解决这些示例。有什么可以帮助我弄清楚我做错了什么吗?

ruby-on-rails

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

Rails Simple_Form 添加年份到日期字段

我是我的 Rails 应用程序,我的User模型的字段之一是出生日期 ( dob)。我在 Simple_Form 中遇到的问题是默认下拉列表只能返回到 2010 年,我需要它进一步返回。

有没有办法可以显示额外的年份?

<%= simple_form_for(@user) do |f| %>
  <%= f.input :dob, order: [:month, :day, :year]
<%= end %>
Run Code Online (Sandbox Code Playgroud)

simple-form ruby-on-rails-4

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

为什么我会收到“目标元素丢失”错误?

我的 Rails 6 应用程序出现错误,因为它找不到我的数据目标,但我不明白为什么。

\n

Chrome\xe2\x80\x99s 控制台中的错误:

\n
Uncaught Error: Missing target element "channel.messages"\n
Run Code Online (Sandbox Code Playgroud)\n

我的看法:

\n
<div class="col-sm-10" data-controller="channel" data-channel-id="<%= @channel.id %>">\n  <!-- Chat messages -->\n  <div class="pr-6 py-4 flex-1">\n    <div data-target="channel.messages">\n      <%= render @channel.messages %>\n    </div>\n        <div class="pb-6 pr-4 flex-none">\n          <div class="flex rounded-lg border-2 border-grey overflow-hidden">\n            <span class="text-3xl text-grey border-r-2 border-grey p-2">\n              <svg class="fill-current h-6 w-6 block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M16 10c0 .553-.048 1-.601 1H11v4.399c0 .552-.447.601-1 .601-.553 0-1-.049-1-.601V11H4.601C4.049 11 4 10.553 4 10c0-.553.049-1 .601-1H9V4.601C9 4.048 9.447 …
Run Code Online (Sandbox Code Playgroud)

stimulusjs

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

基于计算填充Rails字段

在我的Rails 3.2应用程序中,我想基于计算填充一些字段,其中用户输入的字段值是变量.但是,使用我当前的代码,计算似乎只能根据数据库中已有的值工作 - 它在初始保存时无法正确计算,但如果我返回记录并保存一秒,它将正确计算时间.

我的模型中有这四个字段(Trade):

  1. entry_price
  2. exit_price
  3. percent_result
  4. dollar_result

用户创建具有入场价格的交易,然后使用exit_price编辑交易.输入exit_price后,应用程序应计算percent_result和dollar_result.但是,现在,这些结果字段在第一次更新时没有正确填充 - 这似乎是因为它没有从字段中读取exit_price(当用户在表单中输入时),只有在将其保存在D B.

我的控制器出了什么问题?

我的控制器:

def update
  @trade = Trade.find(params[:id])
  exit_price = params[:trade][:exit_price]

  if !exit_price.blank?
    @trade.percent_result = ((exit_price.to_f - @trade.entry_price)/@trade.entry_price) * 100
    @trade.dollar_result = exit_price.to_f - @trade.entry_price 
  end

  params[:trade][:exit_date] = Date.strptime(params[:trade][:exit_date], '%m/%d/%Y') unless params[:trade][:exit_date].blank?
  params[:trade][:entry_date] = Date.strptime(params[:trade][:entry_date], '%m/%d/%Y') unless params[:trade][:entry_date].blank?
  respond_to do |format|
    if @trade.update_attributes(params[:trade])
      format.html { redirect_to @trade, :flash => {:share =>"Your trade was successfully updated.  Don't forget to share it with your friends, so you can profit together!"} …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails-3

2
推荐指数
1
解决办法
2392
查看次数