In my Rails application I have created a controller to get data from database which also includes params in model as follows.
The problem is that there are no html pages in views folder.. Do I need to run the controller??? I want the output i json format.. How are the html pages created and where can i see my json format of data…
# shoppingDemo.rb (controller)
class ShoppingDemo < ApplicationController
def index
@lists=products.all;
respond_to do |format|
format.html
format.json { render json: @lists}
end
end
def show
@products = products.find(params[:prod_id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @products }
end
end
end
# products(model)
class products < Activerecord::Base
attr_accessible :model_name, :brand_name, :price, :discount, :qty_available
end
What is the next step to create html pages or to see my data from database in json format.
You should fix some issues:
1) in Model
class Product2) in Controller
@lists = Product.all@products = Product.find(params[:id])3) You should create routes in
config/routes.rbIt will create routes for index, show, new, create, update, destroy actions. Read more here.
After that you can access your products list through http request on
http://localhost:3000/productsand show your product with id=1 onhttp://localhost:3000/products/1