ruby-on-rails-3

Rails 3: How to find records with field possibly equals to nil?

浪尽此生 提交于 2020-01-03 03:00:32
问题 I would like to find all SongWriter s with maiden_name equals to my_maiden_name ? Note: my_maiden_name may be nil I tried: SongWriter.where("maiden_name = ?", my_maiden_name) and it works fine except the case where my_maiden_name = nil . When my_maiden_name = nil , the generated query is: SELECT `song_writers`.* FROM `song_writers` WHERE (maiden_name = NULL) rather than: SELECT `song_writers`.* FROM `song_writers` WHERE maiden_name IS NULL How could I generalize the active record query to

How to return a the index.html.erb in the js.html.erb file

China☆狼群 提交于 2020-01-03 03:00:12
问题 I have a controller like so: def index respond_to do |format| format.html {} format.js {render :layout => false} end end index.html.erb hello i"m index.html.erb index.js.erb <%= render :template => "dashboard/index" %> What I want to do is have index.html or index.js return the same thing. But I can't see to do that without making index.html.erb a partial (_index.html.erb) but then when a user makes an html call that won't work. How can I get the index.js.erb to render index.html.erb? which I

Subdomaining in rails 3

笑着哭i 提交于 2020-01-03 02:47:05
问题 I am working on a project which is a app store. User can subscribe to many applications. I am creating a subdomain for each user and applications he subscribed are again running on subdomains. Eg. 1). One user registers and selects subdomain as "mysubdomain". Then after logging in he will be accessing everything as "mysubdomain.appstore.com". 2). After registration he subscribes an app, say, "discussion-board". 3). Then the discussion board will be running on "discussion-board.mysubdomain

Rails 3, Heroku: Taps Server Error: PGError: ERROR: invalid byte sequence for encoding “UTF8”: 0xba

牧云@^-^@ 提交于 2020-01-03 02:40:10
问题 I have a Rails 3.0.9 application running both locally in my dev env and remotely on a heroku app. I have a method that imports a CSV file into a model, and this file can contain non-english characters, like °,á,é,í, etc (it's in spanish). I am currently able to import the complete file (75k records) without any problems in my local dev (SQLite) database ; but, when uploading the db to heroku with heroku db:push , it fails with the error I'm posting in the title : !!! Caught Server Exception

Ruby on Rails: Why the confirmation message does not appear in “link_to('delete', …)”?

空扰寡人 提交于 2020-01-03 02:10:25
问题 I have the following link next to one of my products: <%= link_to("Delete", {:action => 'destroy', :id => product.id}, :class => 'action', :confirm => 'Are you sure?') %> but when I click it, the confirmation box does not appear. The generated HTML is: <a data-confirm="Are you sure ?" class="action" href="/products/destroy/48">Delete</a> Please advise. 回答1: <%= link_to("Delete", product, :method => :delete, :class => 'action', :confirm => 'Are you sure?') %> check your javascript_include_tag

How do I get the format of my URLs to be username/controller/:id in Rails 3.1?

旧城冷巷雨未停 提交于 2020-01-03 01:51:55
问题 I want it similar to the way Twitter handles the URLs for its tweets. For instance, right now my URL looks like this: mydomain.com/feedbacks/1/ , where feedbacks is the name of the controller. I want it to look like: mydomain.com/username/feedbacks/1/ which is similar to Twitter's: twitter.com/username/status/:id/ . My routes.rb looks like this: resources :users do resources :feedbacks end When I have it like this, it gives me the URLs as mydomain.com/users/1/feedbacks , but I want the actual

Render failing to render correct template in rescue_from ActiveRecord::Rollback method

瘦欲@ 提交于 2020-01-02 23:59:24
问题 I'm building the checkout page for an e-commerce site, and I have a fairly long transaction that creates a new User model and a new Order model. I wrapped the creation of these models in a transaction so that if validation for one fails, the other isn't hanging around in the database. Here's the trimmed-down code in my OrdersController: rescue_from ActiveRecord::Rollback, with: :render_new def render_new render action: 'new' end ActiveRecord::Base.transaction do @user = User.new params[:user]

Move entire directory and its contents to another location on the same server using Net::FTP in Ruby

99封情书 提交于 2020-01-02 23:58:22
问题 As stated in the title, I would like to move a directory on a FTP Server to some other path on the same server. I want to accomplish this using Net::FTP but other solutions are also welcome. Since there's no proper method for moving files or directories in Net::FTP Documentation, a solution involving copying the directory to another path and deleting the original would be preferable. Please stay on topic and leave solutions related to the question. 回答1: Well, I found the solution and it's

RVM is loading with the wrong version of ruby?

泄露秘密 提交于 2020-01-02 23:38:10
问题 When I open terminal I see: Last login: Sun Aug 26 15:33:57 on ttys001 Using /Users/me/.rvm/gems/ruby-1.9.3-p125 Running /Users/me/.rvm/hooks/after_use Using /Users/me/.rvm/gems/ruby-1.9.3-p194 ruRunning /Users/me/.rvm/hooks/after_use me-MacBook:site me$ ruby -v ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0] Where I want ruby 1.9.3. If I then type: rvm use 1.9.3 --default I get the below: $ rvm use 1.9.3 --default Using /Users/me/.rvm/gems/ruby-1.9.3-p194 Running /Users/me/.rvm

Separating rails logs per action

左心房为你撑大大i 提交于 2020-01-02 23:15:36
问题 I have rails 3 app that generates a lot of requests for analytics. Unfortunately this drowns the logs and I lose the main page requests that I actually care about. I want to separate these requests in to a separate log file. Is there a way to specify certain actions to go to a certain log file? Or possibly a way to reduce the logging level of these actions, and then only show certain level logs when reading back the log file? 回答1: I found this site, which talked about using a middleware for