How do I create a single page application in Rails

2019-07-20 22:16发布

问题:

I'm creating my first Rails 3.2 application after completing the excellent Rails Tutorial.

I would like my application to do this:

Pull a random recipe from the database (I have finished this bit using the 'index' action and this bit of code: @recipe = Recipe.find(:first, :order => 'Random()'))

If the user clicks the 'something similar' button, the app fetches a related recipe from the db

If the user clicks 'something different', the app fetches an unrelated recipe

From what I understand, there should be an action for both 'something similar' and 'something different'. Is this correct?

Should I use a POST form with submit buttons for 'something different' and 'something similar' to determine which action to take?

Also, how can I keep the user on a single page throughout (so they always see the URL as http://www.whatshouldicookfordinner.co.uk rather than /recipes#somethingdifferent)?

回答1:

To keep the user on the same page throughout, you will want to use AJAX. Check out this railscasts, you can apply the concept to what you are trying to do.



回答2:

Use a javascript framework/library (Ember, Backbone, Angular, Knockout, Meteor, Batman, etc..)

Check out this railscast: http://railscasts.com/episodes/323-backbone-on-rails-part-1