Tag: 的Ruby on 轨道

Ruby on Rails – 如何在没有应用程序布局的情况下渲染一个动作?

我有一个Rails应用程序,我想呈现一个行动,而不使用我的应用程序布局(其中有页眉/页脚的东西)。 我怎么能这样做?

为什么闪光消息不会消失?

我正在做一些exception处理在我的控制器,当有一个exception抛出:create action,我将渲染到:新行动,并显示一个flash消息。 一切工作正常,我可以看到exception捕获时的flash消息,但是当我redirect到(handly click)其他页面时,flash消息仍然在这里 。 然后我redirect到另一个页面( 第二次点击) ,消息可能消失。 谁知道是什么原因? 我的控制器代码: class MessagesController < ApplicationController rescue_from Exception, :with => :render_new def new end def create end private def render_new flash[:alert] = t("uploading_error") render :action => :new end end 我的布局代码(Haml): %body #content – unless flash[:alert].blank? #alert= flash[:alert]

如何在模型中使用“number_to_currency”辅助方法而不是视图?

我想在我的模型中使用to_dollar方法,如下所示: module JobsHelper def to_dollar(amount) if amount < 0 number_to_currency(amount.abs, :precision => 0, :format => "-%u%n") else number_to_currency(amount, :precision => 0) end end end class Job < ActiveRecord::Base include JobsHelper def details return "Only " + to_dollar(part_amount_received) + " out of " + to_dollar(price) + " received." end end 不幸的是,这里不能识别number_to_currency方法: 未定义的方法`number_to_currency'为#<Job:0x311eb00> 任何想法如何使其工作?

我如何validation两个字段的唯一性

我需要在添加对象(行)之前validation两个字段的唯一性。 Employee_id和area_id是我emp_area表中的两个字段。 可以有多个具有相同employee_id的logging和具有相同area_id的多个logging,但是没有两个logging可以具有相同的employee_id和相同的area_id。 这有点像构成主键或唯一键的两个字段。 我怎样才能做到这一点。 谢谢

如何重置轨道中的单个表?

我想要主键值从1开始。

在Rails 3中将lib添加到'config.autoload_paths'不会自动加载我的模块

我把一个文件名g.rb放在Rails.root/lib文件夹中。文件内容是这样的: module Google end 然后我补充一下 config.autoload_paths += %W(#{config.root}/lib #{Rails.root}/app/delayed_jobs) 到我的Rails.root/config/application.rb 但是,当我尝试从rails console调用Google时,会引发exception。 只有当我执行require 'google' ,exception消失。 为什么? 我的文件不应该自动加载,不应该访问该模块没有任何额外的require声明?

如何在迁移脚本中设置date时间列的默认值?

考虑下面的表格创build脚本: create_table :foo do |t| t.datetime :starts_at, :null => false end 是否可以将默认值设置为当前时间? 我正在试图在下面给出的SQL列定义的rails中find一个DB独立的等价物: Oracle语法 start_at DATE DEFAULT SYSDATE() MySQL语法 start_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP 要么 start_at DATETIME DEFAULT NOW()

form_for和form_tag之间的区别?

form_for和form_tag有什么区别? form_remote_for和form_remote_tag有什么不同?

Rails路由命名空间和form_for

我有我的routes.rb命名空间 namespace :businesses do resources :registration end 我的控制器在一个子目录business / registration_controller。 def new @business = Business.new end 在我看来,我想做这个form_for @business do |f| … form_for @business do |f| …但我收到以下错误: No route matches {:controller=>"businesses", :action=>"create"} 重新启动服务器,我也得到这个: undefined method business_path'for#<#:0x10339bb20>` 这里是我的耙路线: home_index GET /home/index(.:format) {:action=>"index", :controller=>"home"} new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"} user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"} destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", […]

rails server bin / rails:6:警告:已经初始化常量APP_PATH错误

我已经尝试了一些东西,如卸载/重新安装铁轨和gem,但无济于事。 当我进入我的新项目,并运行rails或捆绑exec rails服务器时,我得到这个错误: bin/rails:6: warning: already initialized constant APP_PATH /Users/toabui/Sites/cms/bin/rails:6: warning: previous definition of APP_PATH was here Usage: rails COMMAND [ARGS] 在我的bin / rails里面我看到这个代码: #!/usr/bin/env ruby begin load File.expand_path("../spring", __FILE__) rescue LoadError end APP_PATH = File.expand_path('../../config/application', __FILE__) require_relative '../config/boot' require 'rails/commands' 有谁知道当我运行rails s时为什么不断收到这个错误? 我GOOGLE了,似乎有一个spring的gem错误,但我似乎无法得到它的工作。