How pagination can be done in ruby on rails 3.1? - ruby-on-rails-3

I heard that Pagination is included automatically for all controllers? How can it be implemented?

As far as I know, there's nothing included automatically. I believe there was built-in pagination in much earlier versions of Rails.
There are (at least) two gems that can be installed to do pagination. The will_paginate gem has been around for years and can be used in Rails 2 or 3. The more recent kaminari gem can be used in Rails 3.

The more recent kaminari gem can be used in Rails 3.
You can refer
https://github.com/amatsuda/kaminari
it is very simple to use.
Put this line in your Gemfile:
gem 'kaminari'
Then do bundle install
Typically, your controller code will look like this:
#users = User.order(:name).page params[:page]
On the views
Just call the paginate helper:
<%= paginate #users %>
Now you can see the paginated result..

As Don Roby said, there are several pagination gems and will_paginate and Kaminari are 2 popular ones. You can find some nice tutorial on pagination (incl. using these 2 gems) here : http://railscasts.com/episodes?utf8=%E2%9C%93&search=pagination

Related

Client-side authentication with emberjs and device in Rails4

Working with user resources is an essential part of every application an so there it's a task which should be automated as much as possible.
As for Ember I found a tutorial https://github.com/heartsentwined/ember-auth-rails-demo/wiki
which describes how it can communicate with devise-based authentication system. And, wow, it's a hell of a boilerplate:)
Have something changed with new devise for Rails4 or things are the same?
ember-auth dev here.
Edit / Update for googlers: I have now built a rails 4 app, with devise, ember, and ember-auth. Apart from the following two gotchas, everything is working fine.
devise >= 3.1 removed the tokenAuthenticatable module. So I'd declare in my Gemfile:
gem 'devise', '>= 3.0', '< 3.1'
ember-data is drifting away from ActiveModelSerializers, towards json-api. Problem is, json-api itself isn't even stable. The quick and easy fix is to replace DS.RESTAdapter with DS.ActiveModelAdapter, which follows the ActiveModelSerializers conventions. It should "just work".
So, yeah, ember-auth does support rails4, because there is nothing BC-breaking with it per se.
(Previous answer:)
I have no experience with rails 4, but ember-auth itself doesn't rely on rails 3, or in fact rails / devise in particular. The only expectation is a set of API that your server exposes.[1] The docs describe the expectation from the server API.
As for using rails as a backend, ember-data explicitly declares support (and adherence to) active_model_seriailzers, which provides convenience methods for churning out json responses from rails models. However, since authentication actions do not conform to the "standard" RESTful model responses, the ember-auth-rails-demo tutorial itself hand-crafts the expected responses. Example:
def create
# ...
data = {
user_id: resource.id,
auth_token: resource.authentication_token,
}
if params[:remember]
resource.remember_me!
data[:remember_token] = remember_token(resource)
end
render json: data, status: 201
end
So, for rails 4 compatibility, I would investigate more on devise compatibility, any ActiveRecord changes, and in general other gem compatibilities as needed. As for ember-auth, it will still be handcrafting the expected responses, as outlined in the docs.
[1]: Even this expectation would be customizable, by writing customized adapters. Advanced usage, but I can elaborate more on this if needed.

How can I contribute to an existing rails gem/engine

What is the best way to extent and contribute to a rails gem/engine
I have found this blog gem/engine , which I want to use it in my rails3 application.
But there are few modifications / features I would like to have and I'm willing do code them. And after that I would like to add them to the original gem/engine (if the author permits)
But I'm confused with how and where to code and test my new changes.
So far I have done the followings
1 - Fork the gem/engine to my github account
2 - clone the source to my local machine
3 - created a sample rails app and added the gem (from my github account)
My question is,
How can I do the code changes to the gem and test them. Gem itself has used rspec and I could do that too, but some of the changes I'm planning (like layout changes), is litttle hard to check with rspec.
this gem in using rails > 3
There is an excellent ScreenCast about it: RailsCasts: Contributing to Open Source

binding.pry ignored by rails 3

I'm trying to debug my little app failing to create a Price because of an AssociationTypeMismatch. So I dig out pry, install, watch railscast and Josh's screencast and dig into the wiki.
I "cd" into the prices controller but that fails. I've put <% binding.pry %> into the new.html before the submit code but Rails says that the 'pry' method is unknown. pry is in my updated Gemfile and I restarted the server.
I put binding.pry into the create method of my prices_controller.rb and Rails happily breezes through it to throw the error I've already seen, not the pause, etc I was expecting.
Looking at the Price model, I'm not experienced enough to know what I should be looking for. What am I overlooking? thanx, sam
I have the exact same problem, my solution was to add pry-remote to my Gemfile, and call binding.remote_pry instead of binding.pry, open the terminal at your current project and execute pry-remote. happy debugging :)
It appears that in addition to the pry gem, I need the pry-rails gem as well. Now the binding.pry is recognized.
Instead of installing pry-rails you could just require 'pry' before the binding :-)

mongomapper rails 3 and devise

I am using mongomapper throughotu my rails 3 app but am using authlogic and active record for authentication and storing users.
I am now changing this to devise and mongomapper like the rest of my app.
I know devise seems to now support mongoid but i don't want to mix mongid and mongomapper and now that mongomapper uses active modle it should work with devise.
I have seen some forks but they don't seem active since earlier this year.
What is the recommended way to use devise with MM now ? Isnt there just a orp adapter i now ? or do i use a fork of devise ?
cheers
Rick
Take a look at Kristian Mandrup's MM-Devise adapter: https://github.com/kristianmandrup/mm-devise
Hopefully it still works with more recent versions of Devise/MM.
I just used the mm-Devise adapter described above and it works great... You have to do some tweaking to get it to work as the documentation seemed a little out of date when used with ruby 1.9.3 and Rails 3.2.

Rails 3 CSRF_meta_tag not functioning

I am in the process of upgrading my app from Rails 2.3 to Rails 3. I read that I need to insert
<%= csrf_meta_tag %>
in my layouts, which I did. Unfortunately, when I used ajax requests, I kept being logged out. After a little research, I found an older way was to add
$j(document).ajaxSend(function(e, xhr, options) {
var token =$j("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
in my application.js file. It now works properly (It seems I actually don't need the csrf_meta_tag).
Is it normal I had to add these 4 lines of code in my application.js instead of just the csrf_meta_tag, or am I just missing something?
Firstly, you do need csrf_meta_tag. If you don't use it in your layout the csrf-token meta will not be generated then your application.js fix stops working.
The jquery-ujs gem (jquery-rails) should take care of this for you. It's possible the version you use have some bug. (I do remember there's a related bug in 3.0.x, but couldn't recall the exact issue). Try updating jquery-ujs and re-generate jquery (rails generate jquery:install), remove rails.js to see if the issue be fixed.
Bonus note: as of rails 3.1, csrf_meta_tag has been renamed to csrf_meta_tags, but the former one could still be used.
Please see jquery-ujs on github.