我的应用程序中有字段hr和min整数.对于hr字段,如果用户输入"1",我希望Rails在将其保存到数据库之前自动将其填充为"01".对于该min字段,如果用户输入"0",则应输入"00".
我怎样才能做到这一点?
我在Rails 3应用程序中使用Devise,但在这种情况下,用户必须由现有用户创建,该用户确定他/她将拥有哪些权限.
因此,我想要:
我怎样才能做到这一点?
目前,我通过以下方式有效地删除了这条路线devise_for :users:
match 'users/sign_up' => redirect('/404.html')
Run Code Online (Sandbox Code Playgroud)
这有效,但我想有更好的方法,对吧?
正如Benoit Garret所说,在我的情况下,最好的解决方案是跳过创建注册路线,并创建我真正想要的那些.
为此,我首先运行rake routes,然后使用输出重新创建我想要的.最终结果如下:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
Run Code Online (Sandbox Code Playgroud)
注意:
:registerable在我的User模型devise/registrations 处理更新电子邮件和密码删除默认Devise路径的路由; 即:
devise_for :users, path_names: {
sign_up: ''
}
Run Code Online (Sandbox Code Playgroud) 我正试图像Stripe那样对我的API进行版本测试.下面给出了最新的API版本是2.
/api/users 返回301到 /api/v2/users
/api/v1/users 在版本1返回200个用户索引
/api/v3/users 返回301到 /api/v2/users
/api/asdf/users 返回301到 /api/v2/users
因此,除非指定的版本存在,否则基本上任何未指定版本的内容都会链接到最新版本,然后重定向到它.
这是我到目前为止:
scope 'api', :format => :json do
scope 'v:api_version', :api_version => /[12]/ do
resources :users
end
match '/*path', :to => redirect { |params| "/api/v2/#{params[:path]}" }
end
Run Code Online (Sandbox Code Playgroud) 我有这样的循环:
<% @posts.each do |post| %>
<% render middle %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后在我的中间部分,我如何访问当前的帖子?
页面网址就像/people?search=name
我使用current_path的水豚方法一样/people只返回.
current_path.should == people_path(:search => 'name')
Run Code Online (Sandbox Code Playgroud)
但它没有说
expected: "/people?search=name"
got: "/people"
Run Code Online (Sandbox Code Playgroud)
我们怎么能通过?有没有办法做到这一点?
我有一个同事正在积极地试图说服我不要使用do..end而是使用花括号来定义Ruby中的多行块.
我坚定地只使用花括号作为短单线,并且做其他事情.但我想我会联系更大的社区来获得一些解决方案.
那是哪个,为什么?(一些应该代码的示例)
context do
setup { do_some_setup() }
should "do somthing" do
# some more code...
end
end
Run Code Online (Sandbox Code Playgroud)
要么
context {
setup { do_some_setup() }
should("do somthing") {
# some more code...
}
}
Run Code Online (Sandbox Code Playgroud)
就个人而言,只看上面的问题就可以解答我的问题,但我想向更大的社区开放.
我在Ubuntu 11.10和RubyMine上运行我的开发
这是我对database.yml的开发设置:RubyMine为我创建的
development:
adapter: postgresql
encoding: unicode
database: mydb_development
pool: 5
username: myuser
password:
Run Code Online (Sandbox Code Playgroud)
当我尝试运行应用程序时,我在下面收到此错误,似乎我还没有创建"项目"用户,但是,我如何创建用户并在postgres中为其授予数据库?如果这是问题,那么,在Ubuntu中为此任务使用的推荐工具是什么?如果这不是问题,那么,请指教.
Exiting
/home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `initialize': FATAL: Peer authentication failed for user "project" (PG::Error)
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `new'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `connect'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:303:in `new_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:313:in `checkout_new_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `block (2 levels) in checkout'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `loop'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `block in checkout'
from /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in …Run Code Online (Sandbox Code Playgroud) 我有一个Rails应用程序,我试图在生产环境中测试.我跑了RAILS_ENV=production rake assets:precompile,它在/ public/assets中生成了我的所有资产.问题是,当我启动我的应用程序时,RAILS_ENV=production rails s thin我得到:
ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):
Run Code Online (Sandbox Code Playgroud)
这个文件确实存在于/public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.
有什么想法为什么我得到这个RoutingError?
我正在浏览Rails API文档collection_select,它们太可怕了.
标题是这样的:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
这是他们提供的唯一示例代码:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
有人可以解释一下,使用一个简单的关联(比如一个Userhas_many Plans,一个Plan属于一个User),我想在语法中使用什么,为什么?
编辑1:此外,如果你在一个form_helper或一个常规形式内解释它是如何工作的,那将是很棒的.想象一下,您正在向了解Web开发的Web开发人员解释这一点,但对Rails来说是"相对较新的".你会如何解释它?
我有这个代码:
<%= link_to image_tag("facebook.png", :class => "facebook_icon", :alt => "Facebook", :target => "_blank"),
"http://www.facebook.com/mypage" %>
Run Code Online (Sandbox Code Playgroud)
当用户点击链接时,如何在新标签页中将其打开?
ruby-on-rails-3 ×10
ruby ×4
capybara ×1
coding-style ×1
devise ×1
postgresql ×1
regex ×1
routes ×1
routing ×1
rspec ×1
ubuntu-11.10 ×1
versioning ×1