Showing posts with label Rails. Show all posts
Showing posts with label Rails. Show all posts

Monday, 1 July 2013

Cancan Ability

In your gemfile Include.
1. gem "cancan" 
2. install bundle. 
3. rails g cancan:ability
this will generate an ability class in your models. 
define your Abilities there like below.
but keep remember that you have already defined roles, 
such as you have a User model,
having two roles defined i.e admin and support.

class Ability
include CanCan::Ability
def initialize(user)
user||= User.new
can :read, :all
if user.role == 'admin'
can :manage, :all
else
can :read, :all
end
end
end

4. the resource on which you want to restrict a user,
 use the following filter in their controller.
                  load_and_authorize_resource
5. if you want restrict something in the views not to show.
<% if can? :manage, @flower %>
<td><%= link_to 'Edit', edit_flower_path(flower) %></td>
<% end %>
<% if can? :manage, @flower %>
<td><%= link_to 'Destroy', flower_path(flower),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
 




 


Using Bootstrap with Rails 3.2

 Useful Resource :

http://railsapps.github.io/twitter-bootstrap-rails.html

Tuesday, 25 June 2013

Michael Hartl tutorial Chapter 7. Error: " expected css "title" with text "Michael Hartl" to return something"

While passing the test for the show method of user, the test fail due to the following error.
Failures:

  1) User pages profile page
     Failure/Error: it { should have_selector('title', text: user.name) }
       expected css "title" with text "Michael Hartl" to return something
     # ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'

Finished in 2.22 seconds
33 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:18 # User pages profile page
This is because of the helper tag in "app/views/users/show.html.erb"
 Replace
<% provide(:title, @user.name) %>
with 
 <title><%= @user.name%></title>

Monday, 24 June 2013

Static pages should have the right links on the layout

While practising Michael Hartl's Rails tutorial, when I want to test the links. One test is failing with the following error message.
Error: "Static pages should have the right links on the layout
This can be fixed by putting "visit root_path" before  click_link "Sign up now!"
screen shot of the code is given below.
___________________
it "should have the right links on the layout" do
    visit root_path
    click_link "About"
    page.should have_selector 'title', text: full_title('About Us')
    click_link "Help"
    page.should # fill in
    click_link "Contact"
    page.should # fill in
    click_link "Home"
    visit root_path
    click_link "Sign up now!"
    page.should # fill in
    click_link "sample app"
    page.should # fill in
  end
____________________


 Useful Links :

Tuesday, 30 April 2013

Rails Restfull API/Rails json/xml api

In Controllers => api => V1
create "users_controllers.rb"
the code inside this controller may seems something like following
class Api::V1::HotelsController < ApplicationController
  def show
  @user = user.find( params[:id] )
  respond_to do |format|
        format.json {render :json => @user}
        format.xml {render :xml => @user}
    end
 end
end
_____
Now go to routes and Add the following
namespace :api do
    namespace :v1 do
      resources :users       
    end
   end
____________
now run $ rake routes | grep users
and check the path against the show method. this will seem something like following.
"/api/v1/users/:id"
---
Now hit http://localhost:3000/api/v1/users/(any ID like 1,2 or 3).json

Friday, 26 April 2013