ruby-on-rails-3

What's the recommended way to rebuild a page from the browser history? (History.js+Rails 3)

安稳与你 提交于 2019-12-23 03:38:22
问题 Many of the pages in my Rails 3 application are composed by widgets. For example there is a user page with url /users/12 that invokes the users controller show action. This page loads the feed widget that makes an ajax call to the feed controller in order to get all the feed items for the user. So lets' suppose the user does the following: 1) enters in the browser http://mydomain.com/users/12 2) clicks on a button "Today's posts" to see just today's post (this triggers an ajax call to the

Arel: order by association count

本秂侑毒 提交于 2019-12-23 03:37:13
问题 Here is what I'm trying to do class Question has_many :votes end class Vote belongs_to :question end I want to find all questions ordered by the number of votes they have. I want to express this in Arel (in Rails 3) without using any counter caches. Is there any way of doing this ? Thanks. 回答1: Try this: Question.select("questions.*, a.vote_count AS vote_count"). joins("LEFT OUTER JOIN ( SELECT b.question_id, COUNT(b.id) AS vote_count FROM votes b GROUP BY b.question_id ) a ON a.question_id =

Rails how to store passwords for other services?

妖精的绣舞 提交于 2019-12-23 03:37:06
问题 What is the safest way of storing passwords that is used to other services? Example: App that needs to store passwords for other sites to do a rake task for fetching data that require authentication. 回答1: You would store this data as environment variables. For example, for one of my projects I need my app to have access to a Github login. This Github login is stored as an environment variable by adding the following line to my .bashrc file. export GITHUB_LOGIN=myLogin In my app's config file

rails virtual attributes won't read from form_for submission

爷,独闯天下 提交于 2019-12-23 03:32:18
问题 I am trying to implement a rails tagging model as outlined in Ryan Bate's railscast #167. http://railscasts.com/episodes/167-more-on-virtual-attributes This is a great system to use. However, I cannot get the form to submit the tag_names to the controller. The definition for tag_names is : def tag_names @tag_names || tags.map(&:name).join(' ') end Unfortunately, @tag_names never gets assigned on form submission in my case. I cannot figure out why. SO it always defaults to tags.map(&:name)

How to include associated objects using gon in Rails/jQuery

耗尽温柔 提交于 2019-12-23 03:31:59
问题 I have a note and it has a certain number of attributes. It also has associated tags. I have been using gon to make this variable available to the javascript files of a page (making @note.attributes available to this page). I can output the note attributes easily ie if I instantiate: gon.note = @note.attributes I can output its content: alert(gon.note.content) But, this doesn't put the notes' tags through.. I can't, for example, do : alert(gon.note.tags) What if I want to display a note's

How to include associated objects using gon in Rails/jQuery

房东的猫 提交于 2019-12-23 03:31:59
问题 I have a note and it has a certain number of attributes. It also has associated tags. I have been using gon to make this variable available to the javascript files of a page (making @note.attributes available to this page). I can output the note attributes easily ie if I instantiate: gon.note = @note.attributes I can output its content: alert(gon.note.content) But, this doesn't put the notes' tags through.. I can't, for example, do : alert(gon.note.tags) What if I want to display a note's

How to create a relationship of a new parent with an existing child in Rails

一笑奈何 提交于 2019-12-23 03:16:20
问题 My problem is that I have a order(child record) but the user(Parent record) doesn't exist yet. And I would like to send the order as a nested attribute in the user form. Finding the order: @order = Order.where(:paypal_express_token => params[:token]).last Creating the user: @user = User.new Trying to create the relationship if @order.user_id.blank? @order.user_id = @user.id end But it seems that it is wrong. When creating children and parent at the same time, I can do this: @user = User.new

How to add a new custom resource to routes Rails 3

僤鯓⒐⒋嵵緔 提交于 2019-12-23 03:15:47
问题 How do I add a custom route for a new resource in the Rails 3 routes? I know how to do it for collections and members but this style doesn't seem to be working for new resources. Is this a bug or am I doing something wrong? So these work: collection do get :wish end member do get :wish end But this doesn't work: new do get :wish end 回答1: Try this: resources :<resource name> do member do get '<custom action>' end end As an example lets see you have a controller called 'main' and if you have a

(postgreSQL error) FATAL: role “demo” does not exist (PG::Error)

我的未来我决定 提交于 2019-12-23 03:15:16
问题 So I've just installed postgreSQL with HomeBrew and have initialized my new demo_app with the: rails new demo_app -d postgresql command. Albeit, I am getting the following error when starting up the server rails s . Snippit : /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `initialize': FATAL: role "demo" does not exist (PG::Error) Full log: /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active

Devise - current_user is author, Rails 3.0

偶尔善良 提交于 2019-12-23 03:14:34
问题 Iam using Rails 3.0 and Devise. I am trying to figure out the best practice to make the current_logged in user automatically the "author" of a Post. Do I use a hidden form field? Or can I somehow apply this logic in the controller? -- So my example: Bob is logged in and he creates a Post on the site. When he creates the post he doesn't have to fill in the Author field, it simply uses the "current_user" that devise provides the application layout template. I have looked for a straight answer,