Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new page into Rails 4 Application

I have new Rails project, in the project I have a controller, view and model named as Customer. Now, what I need is that beside the CRUD actions I need to add two new pages like:

1. http://0.0.0.0:3000/Customer/sale

2. http://0.0.0.0:3000/Customer/lease

And I want to insert code in these files. How do I achieve this I mean creating new sale and lease links?

like image 208
Praveen George Avatar asked Jan 08 '23 12:01

Praveen George


2 Answers

In your routes.rb file you can add these two routes:

  resources :customers do
    collection do
      get 'create_sale' => 'customers#create_sale', as: :create_sale
      get 'create_lease' => 'customers#create_lease', as: :create_lease
    end
  end

Then, you can add two new methods (actions) in your customers_controller.rb file:

  def create_sale
    # your logic goes here
  end

  def create_lease
    # your logic goes here
  end

and also create two views for them in the app/views/customers/ directory as create_sale.html.erb and create_lease.html.erb where you will put your view related code.

like image 165
K M Rakibul Islam Avatar answered Jan 17 '23 21:01

K M Rakibul Islam


#config/routes.rb
resources :customers do
   %w(sale lease).each do |route|
      get route.to_sym, action: route.to_sym, as: route.to_sym
   end
end

This will give you the ability to call the following controller actions:

#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
    def sale
    end

    def lease
    end
end

Nested Resources

As an aside, there is something you should consider.

I mean creating new sale and lease

If you want to create new sales or leases for your Customer, you may be better looking into nested resources.

You must remember that Rails is object orientated. The controllers are meant to give you the ability to CRUD specific objects - IE create, edit, update, destroy, etc.

--

I see many people asking how they can "add" methods to controllers. This is not a problem. Problem rise, however, when you're trying to include create methods in a scope where they don't belong.

You need to keep your application as modular as possible. As such, you need to be able to extend any functionality into their correct places:

#config/routes.rb
resources :customers do
    resources :leases, controller: :purchases, {type: :lease}
    resources :sales, controller: :purchases, {type: :sale}
end

#app/controllers/purchases_controller.rb
class PurchasesController < ApplicationController
   def new
      @customer = Customer.find params[:customer_id]
      @purchase = @customer.purchases.new(type: params[:type])
   end
   def create
      @customer = Customer.find params[:customer_id]
      @purchase = @customer.purchases.new purchase_params
   end

   private

   def purchase_params
      params.require(:purchase).permit(:type, :customer_id :etc, :etc)
   end
end

#app/models/purchase.rb
class Purchase < ActiveRecord::Base
   belongs_to :customer
end

#app/models/customer.rb
class Customer < ActiveRecord::Base
   has_many :purchases
end

If you felt really adventurous, you'd be able to use STI (Single Table Inheritance) to create different instances of the same class type for Sale and Lease:

#app/models/sale.rb
class Sale < Purchase
end

#app/models/lease.rb
class Lease < Purchase
end

I could explain more about this if you wanted.

like image 40
Richard Peck Avatar answered Jan 17 '23 21:01

Richard Peck