Skip to content

Instantly share code, notes, and snippets.

@baothi
Last active November 4, 2015 15:02
Show Gist options
  • Save baothi/42a468c9d8aba0583063 to your computer and use it in GitHub Desktop.
Save baothi/42a468c9d8aba0583063 to your computer and use it in GitHub Desktop.
làm CRUD trong 1 file index
= form_for [:admins,@admins_product] do |f|
- if @admins_product.errors.any?
#error_explanation
%h2= "#{pluralize(@admins_product.errors.count, "error")} prohibited this admins_product from being saved:"
%ul
- @admins_product.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.field
= f.label :price
= f.text_field :price
.field
= f.label :image
= f.file_field :image
.field
= f.label :status
= f.check_box :status
.field
= f.label :viewed
= f.number_field :viewed
.actions
= f.submit 'Save'
-# #comments_content= render 'form'
%h1 Listing admins_products
%table
%thead
%tr
%th Name
%th Price
%th Image
%th Status
%th Viewed
%th
%th
%th
%tbody
- @admins_products.each do |admins_product|
%tr.product
%td= admins_product.name
%td= admins_product.price
%td= image_tag admins_product.image_url(:thumb).to_s if admins_product.image?
%td= admins_product.status
%td= admins_product.viewed
%td= link_to 'Show', [ :admins ,admins_product]
%td= link_to 'Edit', edit_admins_product_path(admins_product)
%td= link_to 'Destroy', [:admins,admins_product], :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Product', new_admins_product_path
class window.Product
constructor: ->
# alert ("you are working on coffeescript")
@clickonrow()
clickonrow: ->
$(document).on "click",'tr', '.product',(e) ->
id_value = $("#id").val()
name_value = $("#name").val()
price_value = $("#price").val()
image_value = $("#image").val()
status_value = $("#status").val()
viewed_value = $("#viewed").val()
$.ajax
type : "GET"
url: "products"
dataType: "JSON"
data: {
product: {
id: id_value,
name: name_value,
price: price_value,
image: image_value,
status: status_value,
viewed: viewed_value
}
}
success: (data) ->
alert data["id"]
return false
# $('input[name]="name_value"').val(data[name]);
# $('input[price]="price_value"').val(data[price]);
# $("#comments_content").html data
error: (data) ->
# return false
class Admins::ProductsController < ApplicationController
before_action :set_admins_product, only: [:show, :edit, :update, :destroy]
# before_action :set_admins_product, only: [:index]
# GET /admins/products
# GET /admins/products.json
def index
@admins_product = Product.new
@admins_products = Product.all
respond_to do |format|
format.html { }
# format.json { render json: @admins_products}
# format.js
format.json do
id = params[:id]
end
end
end
# GET /admins/products/1
# GET /admins/products/1.json
def show
@product = Product.find(params[:id])
respond_to do |format|
format.json {render json: @product}
end
end
# GET /admins/products/new
def new
@admins_product = Product.new
end
# GET /admins/products/1/edit
def edit
end
# POST /admins/products
# POST /admins/products.json
def create
@admins_product = Product.new(admins_product_params)
respond_to do |format|
if @admins_product.save
format.html { redirect_to admins_products_path, notice: 'Product was successfully created.' }
format.json { render :index, status: :created, location: @admins_product }
else
format.html { render :new }
format.json { render json: @admins_product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /admins/products/1
# PATCH/PUT /admins/products/1.json
def update
respond_to do |format|
if @admins_product.update(admins_product_params)
format.html { redirect_to admins_products_path, notice: 'Product was successfully updated.' }
format.json { render :index, status: :ok, location: @admins_product }
else
format.html { render :edit }
format.json { render json: @admins_product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /admins/products/1
# DELETE /admins/products/1.json
def destroy
@admins_product.destroy
respond_to do |format|
format.html { redirect_to admins_products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admins_product
@admins_product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def admins_product_params
params.require(:product).permit(:name, :price, :image, :status, :viewed)
end
end
[{"id":1,"name":"12","price":"1.0","image":{"image":{"url":"/uploads/product/image/1/hinh-nen-em-be-de-thuong-nhat-1.jpg","large":{"url":"/uploads/product/image/1/large_hinh-nen-em-be-de-thuong-nhat-1.jpg"},"thumb":{"url":"/uploads/product/image/1/thumb_hinh-nen-em-be-de-thuong-nhat-1.jpg"}}},"status":true,"viewed":1,"url":"http://localhost:3000/admins/products/1.json"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment