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

Monday, 29 July 2013

Restricting a page view to a specific action

Like you have a Nav bar on all pages and you want it to be different on Index Pages.

<div id="subheader" class="<%= 'slider-bg' if (controller_name.eql?('home') && action_name.eql?('index') )%>" >

Start working with an existing project

  1. Replace keys in shh Directory
  2. Set username and email
  3. Make a branch
  4. Clone
  5. Create a gemset
  6. Bundle install
  7. Run migration

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 %>
 




 


Heroku Push and Assets precompile


  1. git push heroku master
  2. heroku run rake assets:precompile

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, 14 May 2013

Tuesday, 30 April 2013

Customizing JSON API to return few records

just Add the following method in to the Model ...
def as_json(options = {})
    {
      name: self.name,                # Whatever you want to show
      city: self.city,
      country: self.country,
      customer_id: self.customer_id
   
         }
  end
--------------------------OR---------------------------------------
you can call the .select method in the API controller
like
class Api::V1::YoursController < ApplicationController
  def show
  @your = Your.select('one, two').find(params[:id])
  respond_to do |format|
        format.json {render :json => @your}
        format.xml {render :xml => @your}
    end
 end
end