Tag: ruby

PostsController中的NoMethodError #create undefined方法`latitude =’“将lat和long保存到连接的表!!”

我试图从使用paperclip上传的图像的元数据中将这些坐标发布到另一个名为places的表中。“所以坐标转到places表。” 在places表中有列纬度和经度。 提交post后我遇到了这个错误。 错误的突出显示部分是self.latitude = parse_latlong(etc ….)。 post_id是places表中的外键。 这在以前我在邮政表中有纬度和经度时起作用。 但现在我给它自己的表以更好的数据库结构..我只需要知道如何让我的post控制器与我的位置控制器一起工作,如果这是主要问题?? 场所控制器 class PlacesController < ApplicationController before_action :set_post def create @place = @post.places.build(place_params) if @place.save flash[:success] = "coorinates saved" redirect_to :back else flash[:alert] = "Check the form, something went wrong." render root_path end end private def place_params params.require(:place).permit(:continent, :country, :city, :address, :latitude, :longitude) end def set_post […]

如何将url传递给partial / form?

我想将url传递给一个部分的表单。 但是,当前安装程序会生成错误消息: SyntaxError in OrganizationsController#new syntax error, unexpected keyword_do, expecting keyword_end …or(@organization), url=”url” do |f| @output_buffer.safe_appe… … 该错误突出显示部分/表单的第3行,即 两个视图都使用partial / form,为此包括: ## View1: ## View2 (url should point to `def create` in organizations controller): 路线包括: resources :organizations post ‘signup/register’ => ‘organizations#checkout’, as: ‘signup_checkout’ 部分注册表包括: … ???? 在控制器中Def new : def new if (logged_in?) flash[:danger] = […]

更改Rails 3中日期输入的格式?

我在Rails 3中使用text_field进行日期输入,并希望将日期格式化为mm/dd/yyyy 。 虽然我已经找到了一些以这种方式显示日期的解决方案,但我还没有遇到一种可行的方法来接受该格式作为输入。 问题是,在”09/05/2011″类的模糊日期的情况下,Rails将其解释为“5月9日”而不是“9月5日”。 根据mm/dd/yyyy格式让Rails 3解释这些输入的最佳方法是什么?

刮取外部网站但返回“您必须使用支持JavaScript的浏览器与此站点。”错误

我在Ruby on Rails中使用rake任务。 我有一个网站,我需要登录才能抓住网站。 我填写了表格。 form[:login] = user_name form[:pw] = password form.submit 这会返回错误 #<Mechanize::Page {url #} {meta_refresh} {title “You must use a JavaScript capable browser with this site.”} {iframes} {frames} {links} {forms}> 这很奇怪,因为a)我可以为刮板打开JSfunction吗)这是否是网站本身的问题? [编辑]更多信息。 当我看到表格时,它看起来像这样。 需要注意的一点是{action“/system/JSAuth/NoScript.html”}。 我相信这就是我无法提交行动的原因。 有没有办法解决这个问题? #

Ruby(on Rails)语法

我正在回顾“Ruby on Rails 3 Tutorial”并试图在更深层次上理解语法。 以下是我定义的辅助操作的一些示例代码: module ApplicationHelper def title base_title = “Mega Project” if @title.nil? base_title else “#{base_title} | #{@title}” end end end 我的问题是: “#{base_title} | #{@title}” 这条线的结构究竟发生了什么? 在更高的层面上,查找这样的东西的首选来源在哪里?

下载图像并保存

什么是下载图像并保存的最佳方式? 我目前的代码是: temp_file = Tempfile.new “filename”, “#{RAILS_ROOT}/tmp/” temp_file.puts open(path_to_picture, ‘User-Agent’ => ‘Test’).read mimetype = `file -ib #{temp_file.path}`.gsub(/\n/,””) 但看起来,mimetyp还不行。 最好的祝福

更新Rails中的每个Array-Object值

基本上我想更新Rails 5中的Model的每个表列。 str = “abc—def” str.split(‘—‘).map do |a| Foo.where(product_id:1).update_all(bar: a) end 旧对象就像: [ [0] { product_id: 1, …, bar: “xxx”, … }, [1] { product_id: 1, …, bar: “xxx”, … } ] 新应该是这样的: [ [0] { product_id: 1, …, bar: “abc”, … }, [1] { product_id: 1, …, bar: “def”, … } ] 但我得到的是bar: “def”每个人都bar: […]

权限被拒绝从Ruby gem运行方法

我正在使用名为IMGKit的gem,当我在控制台中使用gem的to_img方法时,我收到此错误: IMGKit::CommandFailedError: Command failed: /rubyprograms/search –format jpg http://google.com -: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open3.rb:67:in `exec’: Permission denied – /rubyprograms/search (Errno::EACCES) from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open3.rb:67:in `popen3′ from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open3.rb:53:in `fork’ 我不知道发生了什么事。 这是to_img方法: def to_img(format = nil) append_stylesheets set_format(format) result = nil stderr_output = nil Open3.popen3(*command) do |stdin,stdout,stderr| stdin << (@source.to_s) if @source.html? stdin.close result = stdout.gets(nil) result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding stderr_output = stderr.readlines.join […]

当使用/不使用self时调用本地方法的方式不同。 为什么?

我有一个名为User模型,与UserFilter有一个has_many关系。 UserFilter与User具有belongs _to的关系。 在User模型中,我有一个名为update_user_filters(filter_params_array)的方法update_user_filters(filter_params_array) 这个方法修改user_filters就像这样 def update_user_filters(filter_params_array) new_filters = [] old_filter = user_filters filters_params_array.each do |filter_params| if filter_params[:id].blank? #if the filter does not yet exist new_filters << UserFilter.new(filter_params) end end user_filters = new_filters end 这user_filters设置为user_filters ,但在保存时不会更新db中的user_filters 。 但是,如果我将分配更改为以下内容。 有人可以解释为什么会这样吗? self.user_filters = new_filters 请注意,当我第一次在模型中引用user_filters ,会在db中执行select操作,因此我不确定此本地方法如何与self和不使用self

成功调用ajax后重新运行JavaScript

我有一个删除按钮,其中包含remote:true选项: # _categories.html.erb 我的destroy方法使用json: # categories_controller.rb def destroy @category = Admin::Category.find(params[:id]) @category.destroy save_to_history(“The category \”#{@category.name}\” has been destroyed”, current_user.id) respond_to do |format| # You can’t retrieve the @categories before attempting to delete one. @categories = Admin::Category.all format.json end end 和我的destroy.json.erb文件看起来像: #destroy.json.erb { “html”:” ‘categories’, :content_type => ‘text/html’) %>” } 现在我的问题是我在页面加载时运行了这个JavaScript,删除初始类别按预期工作……直到数据发生变化。 每次用户通过添加新类别或删除类别来更改数据时,我都需要再次运行此方法。 怎么样? 请善待你的帮助,我根本不懂JavaScript! 哈哈 $(function(){ […]