Rails

Full URLs in Rails logs

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is it possible to get the full URLs in the logs of a Rails application? Currently I'm getting something like: Started GET "/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200 but I need to get: Started GET "http://localhost:3000/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200 because this is an app where the domains are important. 回答1: As of Rails 3.2.12, instead of monkey-patching before_dispatch or call_app, there is now a method that receives the request and returns the Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42

Rails activerecord : sum, max and joins

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have two models users and posts . An user can votes and views a post #users id name #posts id count_votes count_views users_id created_at updated_at I want the user who received the most votes and views on his posts from the last 24 hours. The biggest sum of views and votes win. WHAT I TRIED I have this SQL query, it's good but I would like to have the user with the max of votes, this one give me all users and I don't know how to add count_views select u.name as "Name", sum(p.count_votes) from posts p inner join users u on p.user_id = u.id

Rails 4 Nested Form.

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am building a dynamic form for a client. A form has many form questions which has many form answers. As of now, I am able to create everything nicely in Active Admin and have it displaying through the show action on the app interface. Here is the problem I have. I want to display the form title (which is working), along with the form questions (which is working), along with input fields to submit new form answers on the fly (which is the part that is not working). I feel like I have exhausted everything when it comes to nested forms. I

Rails 3.2 asset_host setting ignored

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My production environment - # Code is not reloaded between requests config.cache_classes = true config.assets.enabled = true # Full error reports are disabled and caching is turned on config.consider_all_requests_local = false config.action_controller.perform_caching = true # Disable Rails's static asset server (Apache or nginx will already do this) config.serve_static_assets = true config.static_cache_control = "public, max-age=31536000" # Compress JavaScripts and CSS config.assets.compress = true config.assets.js_compressor = :uglifier

Ruby on Rails: How do I change the behavior of RecordNotFound?

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: When going to on object's show page with an id that doesn't exist, the RecordNotFonud exception is thown. Is there a way I can redirect to some error page, or maybe a different action when this error is thrown? 回答1: You may use rescue_from if you are using Rails 3: class ApplicationController < ActionController :: Base rescue_from ActiveRecord :: RecordNotFound , : with => : render_404 def render_404 respond_to do | format | format . html { render : action => "errors/404.html.erb" , : status => 404 } # and so on.. end end end Yes,

Ruby on Rails 3 + Rspec + Capybara: check response status

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I migrated from Webrat to Capybara and now i get a lot of errors. For example in webrat i could use that in integration test: response . should be_success But Capybara shows that: Failure / Error : response . should be_success NoMethodError : undefined method `success?' for nil:NilClass Is there any method that provides such function? UPD : My spec: require 'spec_helper' describe "Admins" do before (: each ) do @admin = FactoryGirl . create (: admin ) visit '/' click_link "Login" fill_in "Email" , : with => @admin . email fill_in

Rails 3 multiple parameter filtering using scopes

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Trying to do a basic filter in rails 3 using the url params. I'd like to have a white list of params that can be filtered by, and return all the items that match. I've set up some scopes (with many more to come): # in the model: scope : budget_min , lambda {| min | where ( "budget > ?" , min )} scope : budget_max , lambda {| max | where ( "budget < ?" , max )} ...but what's the best way to use some, none, or all of these scopes based on the present params[] ? I've gotten this far, but it doesn't extend to multiple options. Looking

rails authenticate_or_request_with_http_basic

匿名 (未验证) 提交于 2019-12-03 08:51:18
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: in my RoR application i need to protect a page with basic authentication and i want that the credentials are asked every time that a user link to that page. so i added a filter before the operation, like this: before_filter :request_confirm, :only => [:delete_device] and the filter method is: def request_confirm user = User.find_by_id(session[:user_id]) authenticate_or_request_with_http_basic do |nick, pass| nick == user.nickname and pass == user.password end end it's ok, but only the first time because rails save inserted data, so the

Rails 3 - render partial js.erb

匿名 (未验证) 提交于 2019-12-03 08:50:26
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a simple search form that looks something like: app/views/search/index.html.erb <%= form_for @search, :as => :search, :remote => true, :url => {:action => "query"}, :html => {:id => 'search-form'} do |f| %> ... <% end % <div id="search-results"></div> *app/controllers/search_controller.rb* def index @search = SearchCriteria.new end def query @search = SearchCriteria.new(params[:search]) # otherwise, perform search... @summaries = do_search @search respond_to do |format| format.js end end I then have a js.erb template: *app/views

Show a 404 instead of 500 in Rails

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In my rails app I have defined the routes so users can access records like http://mydomain.com/qwe2 But if they type a wrong "qwe2" they get a 500 page. I think a 404 would be more appropriate. How can I change the error page that is shown? Thanks 回答1: The only reason you get a 500 code is if your application throws an exception. This could be due to a missing route, where you do not have anything defined that matches that, or because your controller or view has crashed out. In a production environment you might want to catch all