Tag: ruby on rails 3.2

渲染部分时自动调用控制器方法

我有一个部分需要有一些控制器逻辑运行才能呈现没有问题。 有没有办法将部分与某些控制器逻辑相关联,无论何时渲染它都会运行? 例如,这是我当前的代码: MyDataController: class MyDataController < ApplicationController def view @obj = MyData.find(params[:id]) run_logic_for_partial end def some_method_i_dont_know_about @obj = MyData.find(params[:id]) # Doesn't call run_logic_for_partial end def run_logic_for_partial @important_hash = {} for item in @obj.internal_array @important_hash[item] = "Important value" end end end view.html.erb: Name: Date: “my_partial” %> some_method_i_dont_know_about.html.erb: Name: User: “my_partial” %> _my_partial.html.erb: : 即使未从控制器显式调用该方法, _my_partial.html.erb在呈现run_logic_for_partial时确保调用_my_partial.html.erb […]

RAILS:从Array填充Object

使用任意SQL语句从查询中填充数组 @myarray = myObject.find_by_sql(sql) SQL的设计方式使得每个myarray项与myObject Model具有相同的字段,例如 \#{myObject value: 100, day: 2013-06-15} 理想情况下,我想拥有myObjects数组 填充数据中的数据; 可访问html; 未保存到数据库中。 它可能吗?

rails 3.2在编辑操作上跳过回形针附件存在validation

我是Rails的新手,我正在开发第一个使用paperclip作为附件的rails应用程序,我正在使用validates_attachment :avatar, presence: true附件的存在validates_attachment :avatar, presence: true这对创建操作非常有效,但是当用户使用时正在编辑他的帐户设置并且没有上传新的头像,没有上传头像的错误但我想在编辑时跳过该validation并且仅在编辑时validation如果用户上传新的替换现有的那个。 我也有 validates_format_of :avatar, :with => %r{\.(jpg|jpeg|gif|png)$}i,:unless => Proc.new {|m| m[:avatar].nil?}, :message => “Please upload files with the following extensions only 检查格式只有在存在但似乎没有工作时,你的帮助将非常感激。 谢谢

在rails 3.2中使用nginx而不是rails来提供static_assets

我最近部署了一个基于SpreeCommerce平台的网站。 我正在使用nginx,并希望nginx能够提供我的静态资产。 我遇到的问题是,当用户在网站上为产品上传新照片时,图像看起来可以正确保存(文件位于服务器上),但它们不会被提供给浏览器。 我可以通过将production.rb更改为config.serve_static_assets = true而不是false并将图片重新上传到现有产品来暂时解决此问题。 但是这有资源而不是nginx服务 – 而不是我想要发生的事情。 这是我目前的nginx配置文件: user spree; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; } http { types_hash_bucket_size 512; types_hash_max_size 2048; sendfile on; tcp_nopush on; tcp_nodelay off; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable “msie6”; gzip_proxied any; gzip_min_length 500; gzip_types text/plain text/css application/json […]

如何在Rails 3.2.3中本地化一般错误消息?

我使用以下部分显示Rails 3.2.3中我的大多数模型的错误消息: # _error_messages.html.erb prohibited this from being saved: There were problems with the following fields: 这很有效,直到我决定使用I18n本地化我的应用程序。 我为德语内容创建了一个新文件de.yml ,其中包含了这个(以及其他许多内容): # de.yml errors: &errors format: ! ‘%{attribute} %{message}’ messages: blank: muss ausgefüllt werden template: body: ! ‘Bitte überprüfen Sie die folgenden Felder:’ header: one: ! ‘Konnte %{model} nicht speichern: ein Fehler.’ other: ! ‘Konnte %{model} nicht […]

可以将表单添加到rails中的另一个模型视图中

我有一个非常小的应用程序我在rails中构建。 这是一个简单的重量跟踪应用程序。 我创建了一个具有注册页面的User模型。 用户登录后,会将其重定向到user#show视图。 到目前为止,这是用户控制器: class UsersController < ApplicationController before_filter :authenticate_user! def show @user = current_user end end 我有2个其他模型,一个是Weight模型,另一个是Goal模型,我想它,所以当用户注册时,他们会看到一个屏幕,要求他们输入当前体重和目标体重这个信息将是分别存储权Weight和Goal模型以及登录用户ID。 到目前为止,我已经能够向用户show.html.erb模板添加一个表单: 这会正确呈现表单但当我单击提交按钮时,它只会转到错误页面,说明Unknown action- The action ‘update’ could not be found for UsersController 。 我假设iM做错了,因为它应该尝试发送到创建操作。 有没有人可以帮助我回到正确的道路上,我非常喜欢铁轨上的菜鸟。

为什么Rails会改变我文本中首字母的大小写?

当我在UI中显示字符串时,Rails 3.2正在改变这个: “10020大街” 至 “10020主要街道” 我该如何避免这种情况? 提前致谢!

在模式中的两条路径上导航关系

从上到下,我的表之间有belongs_to关系,而另一方面则有has_many。 ReportTarget Report Manager Organization 和 Score Manager Organization 所以请注意, Report表和Score表是同一级别的。 他们都有Manager表作为他们的父母。 单独地,我可以弄清楚如何通过急切加载来导航它们。 首先,我会做: @blah = Organization.includes(managers: { reports: :report_targets }).find(params[:id]) 而对于第二个,我可以这样做: @blah = Organization.includes([managers: :scores]).find(params[:id]) 但是因为我在我的控制器中做了并且想要将JSON传递给JBuilder,我不知道如何传递它们? 或者将它们组合在一起? 这样产生的散列会将它们放在一个散列中,但使用单独的键: { “firstoneinfo” : [ # stuff that first json returns, can have their own internal hashes ], “SecondOneinfo : [ #stuff that second json returns, can […]

如何在工厂中使用’sequence’来避免使用’FactoryGirl.reload’?

必须使用FactoryGirl.reload可能会花费一些开销来运行所有测试(当存在许多测试时),并且它也被描述为反模式 。 如何继续使用唯一字段的排序,然后测试正确的值? 你可以在我的代码中看到…… 我必须调用FactoryGirl.reload以便在任何先前的测试已经运行的情况下,增量从1开始。 我必须声明:count => 1因为只有一个该值的实例。 规格/工厂/ clients.rb FactoryGirl.define do factory :client do name “Name” sequence(:email} { |n| “email#{n}@example.com” } sequence(:username) { |n| “username#{n}” } end end 规格/客户/ index.html.erb_spec.rb require ‘spec_helper’ describe “clients/index” do before do FactoryGirl.reload (1..5).each do FactoryGirl.create(:client) end @clients = Client.all end it “renders a list of clients” do render […]

在编写“新”方法时,继续获得“无法找到没有ID的Model_name”

作为背景,我目前有三个模型, School , Course和Section ,他们都是一对多的关系(学校has_many课程,课程has_many部分,相应的belongs_to关系也建立在模型中)。 我还有以下资源(稍后要设置的排除项): resources :schools do resources :courses end resources :sections #not part of the nest 虽然sections可以作为嵌套资源的一部分,但我保留了它,因为Rails指南强烈建议嵌套只有一层深。 所以,我的麻烦在于创建一个新的部分(在SectionsController ),并通过course_id将其链接到课程 def new @course = Course.find(params[:id]) #this line results in an error @section = @course.sections.new end 第一行总是引发“无法找到没有ID的课程”错误,尽管尝试了各种不同的使用组合:id,:course_id等,但我无法通过该错误。由于Course是嵌套资源,因此还有别的东西让我失踪? 谢谢你的帮助! 运行rake routes ,输出如下: sections GET /sections(.:format) sections#index POST /sections(.:format) sections#create new_section GET /sections/new(.:format) sections#new edit_section GET […]