ActionController

I m pretty new to Rails.
Can you help me in this…

ActionController: Create a controller that will find a specific as well
as all orders of a given customer.

Consider there is a table names Order and Customer.
Can you help me with the code?

ActionController wrote:

I m pretty new to Rails.
Can you help me in this…

ActionController: Create a controller that will find a specific as well
as all orders of a given customer.

Consider there is a table names Order and Customer.
Can you help me with the code?

Hi,

First you need to create a controller: “script/generate controller Order
list view”
Then, create two models: “script/generate model Order” and
“script/generate model Customer”

Add a foreign key in your order model (if you use migration, just add "
t.column :customer_id, :integer, :null => false" in your
db\migrate\001_create_orders.rb file. Then run a “rake migrate”. You db
will be set up. Do not forget to add “belongs_to :customer” in your
order model.

Then in your controller:
def list
@orders = Order.find_by_customer_id(params[:id])
end

def view
@order = Order.find(params[:id])
end

Then you need to fill up your views, list.rhtml can contain:
<% @orders.each do |o| %>
Order: <%= o.id %>

<% end %>

and view.rhtml can contain:
Order: <%= @order.id %>

Customer: <%= @order.customer.id %>

Regards,
Jean-Etienne

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs