Tag: ruby on rails 4

用设计用户创建员工

它看起来像Devise用户的Profile模型? 但我的不同。 我想创建员工记录,该员工可以选择是否拥有用户帐户。 我正在为我的用户使用devise gem。 所以我有员工模型 has_one :user accepts_nested_attributes_for :user 和用户模型 belongs_to :employee 我知道我需要将嵌套用户放在我的视图中,但是如何将其保存在控制器中? 这不是这样的,对吗? params.require(:employee).permit(:name, user_attributes: [:email, :password]) 你能给我一些提示或一些有用的链接吗? 修改设计很难。 谢谢^ _ ^

Bootstrap3和简单的forms

以下命令将simple_form与twitter-bootstrap集成在一起 $ rails g simple_form:install –bootstrap identical config/initializers/simple_form.rb create config/initializers/simple_form_bootstrap.rb exist config/locales identical config/locales/simple_form.en.yml identical lib/templates/erb/scaffold/_form.html.erb =============================================================================== Be sure to have a copy of the Bootstrap stylesheet available on your application, you can get it on http://twitter.github.com/bootstrap. Inside your views, use the ‘simple_form_for’ with one of the Bootstrap form classes, ‘.form-horizontal’, ‘.form-inline’, ‘.form-search’ or ‘.form-vertical’, […]

Rails Seed-Fu作家为什么种子被评论出来?

所以,这是我的自定义rake任务: task :backup => :environment do |t| SeedFu::Writer.write(‘/path/to/file.rb’, class_name: ‘Category’, constraints: [:id] do |w| Category.all.each do |x| w << x end end end 以下结果文件包含: # DO NOT MODIFY THIS FILE, it was auto-generated. # # Date: 2014-06-15 21:08:13 +0700 # Seeding Category # Written with the command: # # /home/dave/.rvm/gems/ruby-2.1.2/bin/rake backup # Category.seed(:id, #, #, […]

Rails 4.2:日志中的未知属性或服务器错误

我有一个带有select_tag和options_from_collection_for_select的表单,我似乎无法通过。 在视图中,当上传ID设置为uploadzip_id我得到302重定向,当它设置为uploadzip_ids ,我得到一个Unknown Attribute错误。 我有点困惑,因为我与外键设置了我的关系。 我确实有另一个模型,名为Uploadpdf复选框工作正常。 这是设置.. class Campaign < ActiveRecord::Base has_one :uploadzip end class Uploadzip < ActiveRecord::Base belongs_to :campaign end DB / schema.rb create_table “campaigns”, force: :cascade do |t| t.string “name” t.text “comment” t.datetime “created_at”, null: false t.datetime “updated_at”, null: false create_table “uploadzips”, force: :cascade do |t| t.string “file_name” t.string “file_type” t.datetime “date” t.integer […]

Rails动态错误页面(404,422,500)显示为空白

我正在将动态错误页面实现到应用程序中,并且感觉它正在public文件夹中查找(现在不存在的)模板,而不是遵循我设置的路由。 在config/application.rb我添加了一行config.exceptions_app = self.routes来解释这一点。 我已经将以下内容添加到我的路线中: get “/not-found”, :to => “errors#not_found” get “/unacceptable”, :to => “errors#unacceptable” get “/internal-error”, :to => “errors#internal_error” 而错误控制器看起来像这样: class ErrorsController 404 end def unacceptable render :status => 422 end def internal_error render :status => 500 end end 转到/not-found显示模板应该是,但访问任何不存在的URL(即/ i-dont-exist)呈现空页面。 我能看到的唯一原因是exception处理需要路由,例如, get “/404”, :to => “errors#not_found” ,但具有讽刺意味的是,它没有找到/的路由404(不,这不仅仅是它的工作:))。 任何建议,非常感谢。 谢谢,史蒂夫。

Carrierwave在本地不存储s3上的图像

我使用载波上传图像并将其显示在照片库中。 载波在public/uploads存储文件。 但是这些图像没有在heroku上显示。 我发现heroku是只读的,我们应该在s3存储文件。 除了s3之外还有其他选择吗?如果是的话,请你在这里分享一下吗?

Rails 4 Asset Pipeline:使用和不使用指纹编译

Rails 4改变了资产的指纹识别方式(用于浏览器缓存清除): Old Strategy: application.css?12345 Rails 3.2: application-12345.css and application.css Rails 4.0: application-12345.css 虽然这很有道理,但如果样式表被其他服务(在我们的案例中为Zendesk)使用则会出现问题。 这个外部站点需要来自Rails应用程序的CSS,但它不知道指纹(又名:摘要)。 有没有办法编译带有和没有指纹的所有资产? 或者也许是另一种方法来解决这个问题?

Rails – 如何在不重新加载页面的情况下刷新页面?

Rails – 如何在不重新加载页面的情况下刷新页面? 我正在构建一个应用程序,它具有每次人们访问该页面时都会发出随机数(随机记录)的function,但问题是当用户访问该页面时,会给出一个随机数,但该数字会发生变化再次,如果用户刷新页面。 即使用户刷新页面或返回该页面时,如何保留该随机数,随机数保持不变。 我想在浏览器中禁用刷新function会阻止用户刷新页面,从而阻止他们更改随机数,但在研究之后,看起来禁用刷新function似乎是一个好主意。 有没有其他方法可以实现它? —- —-更新 我试过了 #posts_controller.rb def new @random = cookies[:stuff] ||= Stuff.random end 并且在我的视图中调用@random.content ,因为我只想要Stuff模型的内容,第一次没问题但是当我刷新页面时,它为我提供了undefined method ‘content’ for “#”:String什么事了?

如何在Rails应用程序中覆盖gem生成器模板

我知道如何覆盖gem中的Rails模板,但是如何在Rails应用程序中覆盖gem生成器模板 例如: https : //github.com/elabs/pundit/blob/master/lib/generators/pundit/policy/templates/policy.rb 要么 https://github.com/drapergem/draper/blob/master/lib/generators/rails/templates/decorator.rb 所以rails g decorator Foo会生成我的模板,而不是gem本机模板 谢谢

将current_scopes传递给搜索

我正在使用非常有用的has_scope gem。 我有一个通过在链接中传递范围创建的索引页面。 在那个索引页面上,我可以查询current_scopes。 我在该页面上有一个非常标准的搜索范围搜索表单,但使用它会返回没有任何范围的结果。 如何将current_scopes传递给搜索? 我尝试使用隐藏字段但无法使其工作。 我想如果我可以在一个链接中传递范围,我应该能够以一种forms传递它们但是在谷歌搜索后我不确定这是真的。 搜索表单 {:method => :post} do |f| %> 指数法 def index @user = User.find_by_username(params[:user_id]) @search = apply_scopes(@user.collections).search(params[:q]) @search.sorts = ‘created_at desc’ if @search.sorts.empty? @collections = @search.result.paginate(page: params[:page]) end