Tag: ruby on rails

使用祖先gem添加儿童的表格

我在Rails中有一个用户模型,我正在尝试使用Ancestry gem为用户添加子项。 我希望能够将一个/多个用户ID(子)输入到表单中,然后将它们分配给用户的子项。 我目前有一个用于更新视图中用户名称的表单,但是对Rails不熟悉我正在努力弄清楚表单应该看起来像是让@user的user_ids(子项)父进入表单。 怎么能这样做? edit.html.erb … …

Rails 3.2.13 – 资产不在生产中显示

我花了一整天的时间试图找出这个问题,但没有任何积极的结果。 当我加载我的网站时,没有CSS,图像或工作的JavaScript。 该应用程序在Rails 3.2.13和Capistrano 2上运行。 这是我的设置: 配置/环境/ production.rb Appname::Application.configure do config.cache_classes = true config.consider_all_requests_local = false config.action_controller.perform_caching = true #config.serve_static_assets = false config.serve_static_assets = true config.assets.compress = true config.assets.compile = true config.assets.precompile = [‘*.js’, ‘application.css’, ‘styles.css.scss’, ‘*.css.erb’] config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico) config.assets.digest = true config.cache_store = :memory_store config.i18n.fallbacks = […]

将全局变量放在Rails 3中的位置

我曾经使用我的Rails 2.3.8应用程序将Global变量放在environment.rb中,例如: MAX_ALLOWD_ITEMS = 6 它似乎在Rails 3中不起作用。我尝试将它放在application.rb中并​​且没有帮助。 你有什么建议?

由于sqlite3 gem错误,Heroku部署失败

我刚刚开始阅读Michael Hartl撰写的ruby.railstutorial.org书,并一直在完成第一章。 我正在使用mac book OS X,Terminal和Sublime Text。 一切都按计划进行,直到测试部署到Heroku为止。 我能够连接到Heroku并运行$ git push heroku master命令。 但部署失败: Installing sqlite3 (1.3.5) with native extensions Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /usr/local/bin/ruby extconf.rb checking for sqlite3.h… no sqlite3.h is missing. Try ‘port install sqlite3 +universal’ or ‘yum install sqlite-devel’ and check your shared library search path (the location […]

处理JSON时如何使用nested_attributes?

我正在尝试编写一个处理JSON的更新方法。 JSON看起来像这样: { “organization”: { “id”: 1, “nodes”: [ { “id”: 1, “title”: “Hello”, “description”: “My description.” }, { “id”: 101, “title”: “fdhgh”, “description”: “My description.” } ] } } 组织模式: has_many :nodes accepts_nested_attributes_for :nodes, reject_if: :new_record? 组织序列化器: attributes :id has_many :nodes 节点序列化器: attributes :id, :title, :description 组织控制器中的更新方法: def update organization = Organization.find(params[:id]) if organization.update_attributes(nodes_attributes: […]

了解rails路由:在routes.rb中匹配vs root

我正在关注此链接的rails教程: http : //ruby.railstutorial.org/chapters/filling-in-the-layout#code : static_page_routes 在/config/routes.rb文件中,我有 SampleApp::Application.routes.draw do match ‘/contact’, :to => ‘pages#contact’ match ‘/about’, :to => ‘pages#about’ match ‘/help’, :to => ‘pages#help’ root :to => ‘pages#home’ end 当我运行网站时,它给了我一个错误:没有路由存在页面/主页。 我在论坛周围搜索,ppl建议把匹配’/ pages / home’=>’pages#home’ 我做了: SampleApp::Application.routes.draw do match ‘/contact’, :to => ‘pages#contact’ match ‘/about’, :to => ‘pages#about’ match ‘/help’, :to => ‘pages#help’ match ‘/pages/home’ => […]

如何在Ruby on Rails中进行全文搜索?

我想在Ruby on Rails应用程序中对数据进行全文搜索。 有哪些选择?

在rails3中覆盖model-name的默认复数forms

我的语言环境是:de和我喜欢这个: Sheet.model_name.human.pluralize # => Belegs 给我添加一个尾随“e”而不是“s” Sheet.model_name.human.pluralize # => Belege 仅适用于Sheet-class。 我可以在config / locales / models / de.yml中以某种方式添加它吗?

使用浅路径保护rails API

我正在构建一个JSON api,它将成为另一个rails站点的数据源(使用devise for auth),并且具有基本的令牌认证工作。 API还将负责向移动应用程序提供数据。 在API中,我有以下模型:用户,书籍和章节用户has_many:书籍,书籍has_many:章节。 在我的路线文件中,我有 shallow do resources :users do resources :books do resources :chapters end end end 这导致如下路线: /users/:user_id , /users/:user_id/books /books/:book_id , /books/:book_id/chapters /chapters/:chapter_id 即’浅路线’,而不是/ users /:user_id / books /:book_id / chapters /:chapter_id 我想知道我应该如何validation这些,因为该章不属于用户这不仅仅是检查user_id的情况(因为这是书模型的属性)。 # chapters_controller def show @chapter = Chapter.find(params[:id]) end 我是否必须得到@ chapter的书然后检查它的user_id? 不知怎的,对我来说感觉不对。 我是朝着错误的方向前进的吗? 谢谢阅读!

访问Tableless模型的id值时出现问题

我正在使用Ruby on Rails 3,我在Rails结构中遵循Tableless 模型 ,以便将其应用于我的模型帐户。 一切都有效,但如果我这样做的话 @test = account.id @test的调试导致零值,似乎根本无法访问。 在这个问题的评论中@Wukerplank说: You are right. I suppose id has a special status in ActiveRecord. I think it would only be set after the record is persisted in some database. 我怎样才能检索\访问id属性值? 更新 尝试并重新尝试我发现一个可能的解决方案是使所有属性’attr_accessible’(如果我只是’id’,我得到所有其他值’nil’),但我认为这是一个非常危险的解决方案。 另一个解决方案是创建一个充当id的新类属性,但是如果我已经拥有id ,为什么还要这样做?!