Tag: ruby

遵循Ruby-on-Rails教程并获取“销毁用户”不起作用

我最近安装了Ruby on Rails 3.2并且一直在努力学习它。 我一直在关注RoR 3.0教程(http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#top),到目前为止一切顺利(是的,我知道有一个3.2)版)。 目前我被困在第10.4.2节,该节教导如何添加链接以销毁用户。 它说要添加代码 :delete, :confirm => “You sure?”, :title => “Delete #{user.name}” %> 以及添加apps / view / layout / application / html / erb 看起来这应该是正确的用户控制器中的destroy方法,正如教程所说,但它不适合我,我无法弄清楚为什么。 它创建的链接只是/ user /:id。 我查看了3.2教程中的相同部分,它是相同的方向(但没有javascript包含标记代码)。 我无法按照该教程开始工作。 所以我不确定为什么它不起作用或如何让它工作。 所以我们很清楚,而不是去这个用户控制器中的destroy方法,它转到/ user /:id这是show方法。

无法安装Nokogiri 1.4.3 gem

无法安装nokogiri 1.4.3 gem。 Nokogiri 1.6.6.2安装没有问题。 在Ubuntu上使用最新的RVM。 pm@sniegas:~$ gem install nokogiri -v ‘1.4.3’ Fetching: nokogiri-1.4.3.gem (100%) Building native extensions. This could take a while… ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension. /home/pm/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20150519-7580-2yzgsg.rb extconf.rb *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries […]

设计错误:未定义的方法`current_sign_in_ip’

我刚刚将Devise安装到我的Rails 4.0.10应用程序中。 我希望用户可以跟踪,但我认为我不需要:current_sign_in_ip和:last_sign_in_ip ,所以我删除了这些属性。 我的迁移: ## Trackable t.integer :sign_in_count, default: 0, null: false t.datetime :current_sign_in_at t.datetime :last_sign_in_at t.string :initial_ip # t.inet :current_sign_in_ip # t.inet :last_sign_in_ip 在我尝试创建第一个用户之后,我收到了以下错误: NoMethodError in Devise::RegistrationsController#create undefined method `current_sign_in_ip’ for # 我不明白为什么甚至调用current_sign_in_ip 。 这是“创建”操作,肯定不包括该方法: def create build_resource(sign_up_params) resource.save yield resource if block_given? if resource.persisted? if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_flashing_format? sign_up(resource_name, […]

文件从亚马逊服务器读取,ruby在轨道上,没有匹配路由

我现在正在使用Heroku服务器和AWS开发Ruby on rails应用程序。 在应用程序中,我必须阅读文本文件。 我在本地测试,现在把这个文件放在AWS中。 当我将我的文件地址放在AWS中时,会弹出以下错误消息。 Errno::ENOENT (No such file or directory @ rb_sysopen – https://s3-us-west-2.amazonaws.com/ringleplus2015/my_file.txt): 当我在Chrome上复制并粘贴此地址时,我可以毫无问题地下载文件。 #file = File.open(“./my_file.txt”,”r”) file = File.open(“https://s3-us-west-2.amazonaws.com/testbuckethoho/my_file.txt”,”r”) 我允许任何人阅读这个。 此外,在html.rb文件中,我还放置了用户可以从中下载pdf文件的链接。 “target =”_ blank“class =”btn btn-lg btn-default“>案例下载 但是当我点击它时,它说“这个网页不可用”。 我仔细查看地址,“:”在“https”和“//”之间消失 https//s3-us-west-2.amazonaws.com/testbuckethoho/What+Consumers+Want+from+Their+Displays+%7C+The+Applied+Materials+Blog.pdf 当我在其中包含“:”时,它可以工作。 但是如果我在我的html文件中包含“:”,则会导致我出现此错误。 No route matches [GET] “/home/:/https:/s3-us-west-2.amazonaws.com/testbuckethoho/What+Consumers+Want+from+Their+Displays+%7C+The+Applied+Materials+Blog.pdf” 有人会帮我弄清楚现在导致这个问题的原因并处理它吗? 🙂

Rails以编程方式检测文件的更改

我想编写一个方法,以编程方式检测我的rails应用程序中的任何文件是否已更改。 有可能做一些类似于整个应用程序的MD5并将其存储在会话变量中吗? 这主要是为了获得缓存清单的一些乐趣 。 我已经有一个动态生成的缓存,它在生产中运行良好。 但是在我的开发环境中,每当我更改app目录中的任何内容时,我都希望更新该缓存的id(而不是每10秒,这就是我现在设置它的方式)。 更新 File.ctime(“。”)将是完美的,除了“。”。 更深的目录文件发生更改时,未标记为已更改。 迭代“。”中的所有目录是否有意义。 并将每个的ctimes加在一起?

Rails如何构建MySQL语句?

我有以下代码在一个控制器内的heroku上运行, 间歇性地失败。 它应该对我很有用,但我必须遗漏一些东西。 @artist = Artist.find(params[:artist_id]) 参数hash看起来像这样: {“utf8″=>” “, “authenticity_token”=>”XXXXXXXXXXXXXXX”, “password”=>”[FILTERED]”, “commit”=>”Download”, “action”=>”show”, “controller”=>”albums”, “artist_id”=>”62”, “id”=>”157”} 我得到的错误看起来像这样: ActiveRecord::StatementInvalid: Mysql::Error: : SELECT `artists`.* FROM `artists` WHERE `artists`.`id` = ? LIMIT 1 注意WHERE艺术家. id = ? 声明的一部分? 它试图找到问号的ID。 含义Rails没有传入params[:artist_id] ,这显然是在params哈希中。 我完全失去了。 我在尝试以类似方式选择记录的不同页面上得到相同的错误。 我的环境:Heroku上的Cedar Stack( 这只发生在Heroku上),Ruby 1.9.3,Rails 3.2.8,托管在Amazon S3上的文件(虽然我怀疑它很重要),使用mysql gem(不是mysql2 ,它没有’完全工作),ClearDB MySQL数据库。 这是完整的痕迹 。 任何帮助将非常感激。

Ruby:将数据发布到url

在rails应用程序中,我希望能够在不访问网站的情况下将某些信息发布到方法中的其他网站。 我对这个话题相当新,所以好像我已经5岁了。 我的想法到目前为止: def create_button button = { :name => ‘test’, :type => ‘buy_now’, :callback_url => ‘http://www.example.com/my_custom_button_callback’, :description => ‘sample description’, :include_email => true } “https://thesite.com/api/v1” + button.to_query # post logic here end

如何从通过复选框选择的数组中对项目进行排序?

我想要使​​用复选框对先前选中的项目进行排序。 如果我通过单击复选框选择电影的评级,则会返回适用的电影。 URL的结尾如下所示: ?ratings[PG-13]=1&ratings[G]=1 如果我点击订购电影的链接,它将获取整个电影列表,而不是我刚刚定位的电影列表。 那么,我怎样才能“追加” “sort=title”到url中的评分? 我觉得这是我在视图中可以做的事情。 我的link_to是这样的: %th#title_header{:class=>(“hilite” if @sort == “title”)}= link_to “Movie Title”, :sort => “title” 我的check_box_tag是这样的: = check_box_tag “ratings[#{rating}]”,rating,@all.include?(rating) 在我的控制器中,我有这个: def index @movies = Movie.order(params[:sort]) @movies = @movies.where(:rating => params[:ratings].keys) if params[:ratings].present? @sort = params[:sort] @all = (params[:ratings].present? ? params[:ratings] : []) end (我知道,我的“where”查询容易受到SQL注入的攻击,这是暂时的,我只是想把整个事情概念化。)

如何在侧边栏中停止双重渲染?

代码是双重渲染,如下所示: 什么时候应该只列出一次: Ran 1 miles Apr Journal 1 days Apr 视图/布局/ _stats.html.erb <div class="”> <span class="”> application_controller def set_stats @averaged_quantifieds = current_user.quantifieds.averaged if current_user @instance_quantifieds = current_user.quantifieds.instance if current_user end 结果是Quantifieds的nested_attribute。 quantifieds_controller class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index if params[:tag] @quantifieds = Quantified.tagged_with(params[:tag]) […]

如何以rails方式获取新创建的记录的id?

我有2个型号,假设是model1和model2 。 model2有很多model2 。 model2 属于 model1 。 同时保存model1和model2 。 def create @mod1 = Model1.new(model1_params) id = Model1.last.id + 1 @mod2 = Model2.new(model1_id: id) if @mod1.save && @mod2.save redirect root_path else render ‘edit’ end end 在我删除model1的最后一条记录之前,该解决方案是可以的。 如何在创建之前获取model1的最后一条记录。