-
Notifications
You must be signed in to change notification settings - Fork 22
Week 1 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chenmoneta
wants to merge
11
commits into
rocodev:master
Choose a base branch
from
chenmoneta:week-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Week 1 #16
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d155389
add product controller and model
ccc2251
add new product view
b9a59ee
fix new product view tag
0f2750e
add product edit delete show
a749531
add user auth
ff0269d
add navi bar bootstrap
5763ec0
add image_uploader
09ced8c
add db column price and modify qut default 0
7b6ffab
delete sqlite then db:migrate
bd25867
add admin & user role check
5572c83
add simple form and longin admin check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Place all the behaviors and hooks related to the matching controller here. | ||
| # All this logic will automatically be available in application.js. | ||
| # You can use CoffeeScript in this file: http://coffeescript.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,5 @@ | |
| //= require jquery_ujs | ||
| //= require turbolinks | ||
| //= require_tree . | ||
| //= require bootstrap | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Place all the behaviors and hooks related to the matching controller here. | ||
| # All this logic will automatically be available in application.js. | ||
| # You can use CoffeeScript in this file: http://coffeescript.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the admin::products controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,4 +12,5 @@ | |
| * | ||
| *= require_tree . | ||
| *= require_self | ||
| *= require bootstrap | ||
| */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the products controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| class Admin::ProductsController < ApplicationController | ||
| before_action :authenticate_user! | ||
| before_action :admin_required | ||
|
|
||
| def index | ||
| @products = Product.all | ||
| end | ||
|
|
||
| def show | ||
| @product = Product.find(params[:id]) | ||
| end | ||
|
|
||
| def new | ||
| @product = Product.new | ||
| end | ||
|
|
||
| def edit | ||
| @product = Product.find(params[:id]) | ||
| end | ||
|
|
||
| def destroy | ||
| @product = Product.find(params[:id]) | ||
| @product.destroy | ||
| redirect_to admin_products_path | ||
|
|
||
| end | ||
|
|
||
| def create | ||
|
|
||
| #@product = Product.crate(product_params) | ||
| @product = Product.new(product_params) | ||
| if @product.save | ||
| redirect_to admin_products_path | ||
| else | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def update | ||
|
|
||
| @product = Product.find(params[:id]) | ||
|
|
||
| if | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if 後面要有判斷式 |
||
| @product.update(product_params) | ||
| redirect_to admin_products_path | ||
| else | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def product_params | ||
| params.require(:product).permit(:title, :description, :quantity, :image, :price) | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,23 @@ class ApplicationController < ActionController::Base | |
| # Prevent CSRF attacks by raising an exception. | ||
| # For APIs, you may want to use :null_session instead. | ||
| protect_from_forgery with: :exception | ||
|
|
||
| def admin_required | ||
| if current_user.admin? == false | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以寫 |
||
| render :text => "You can't access this page" | ||
| end | ||
| end | ||
|
|
||
| def after_sign_in_path_for(resource_or_scope) | ||
| if current_user.admin? == true | ||
| admin_products_path | ||
| else | ||
| products_path | ||
| end | ||
|
|
||
| end | ||
|
|
||
| def after_sign_out_path_for(resource_or_scope) | ||
| products_path | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class ProductsController < ApplicationController | ||
| def index | ||
| @products = Product.all | ||
| end | ||
|
|
||
| def show | ||
| @product = Product.find(params[:id]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module Admin::ProductsHelper | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module ProductsHelper | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class Product < ActiveRecord::Base | ||
| mount_uploader :image, ImageUploader | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class User < ActiveRecord::Base | ||
| # Include default devise modules. Others available are: | ||
| # :confirmable, :lockable, :timeoutable and :omniauthable | ||
| devise :database_authenticatable, :registerable, | ||
| :recoverable, :rememberable, :trackable, :validatable | ||
|
|
||
| def admin? | ||
| return is_admin | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # encoding: utf-8 | ||
|
|
||
| class ImageUploader < CarrierWave::Uploader::Base | ||
|
|
||
| # Include RMagick or MiniMagick support: | ||
| # include CarrierWave::RMagick | ||
| include CarrierWave::MiniMagick | ||
|
|
||
| # Choose what kind of storage to use for this uploader: | ||
| storage :file | ||
| # storage :fog | ||
|
|
||
| # Override the directory where uploaded files will be stored. | ||
| # This is a sensible default for uploaders that are meant to be mounted: | ||
| def store_dir | ||
| "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | ||
| end | ||
|
|
||
| # Provide a default URL as a default if there hasn't been a file uploaded: | ||
| # def default_url | ||
| # # For Rails 3.1+ asset pipeline compatibility: | ||
| # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) | ||
| # | ||
| # "/images/fallback/" + [version_name, "default.png"].compact.join('_') | ||
| # end | ||
|
|
||
| # Process files as they are uploaded: | ||
| # process :scale => [200, 300] | ||
| # | ||
| # def scale(width, height) | ||
| # # do something | ||
| # end | ||
|
|
||
| # Create different versions of your uploaded files: | ||
| version :thumb do | ||
| process :resize_to_fit => [150, 150] | ||
| end | ||
|
|
||
| # Add a white list of extensions which are allowed to be uploaded. | ||
| # For images you might use something like this: | ||
| # def extension_white_list | ||
| # %w(jpg jpeg gif png) | ||
| # end | ||
|
|
||
| # Override the filename of the uploaded files: | ||
| # Avoid using model.id or version_name here, see uploader/store.rb for details. | ||
| # def filename | ||
| # "something.jpg" if original_filename | ||
| # end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| <div class="page-header"> | ||
| <h1>Product Edit</h1> | ||
| </div> | ||
|
|
||
| <div class="container col-md-4 col-md-offset-1"> | ||
|
|
||
| <%= simple_form_for @product, :url => admin_product_path, :html=> { role: 'form' } do |f| %> | ||
| <p> | ||
| <%= f.input :title, autofocus: true, label: '商品名稱', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :description, as: :text , label: '商品描述', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :quantity,autofocus: true, label: '商品數量', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :price, autofocus: true, label: '商品價格', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :image, as: :file , label: '商品圖片' %> | ||
| <br> | ||
| <%= image_tag @product.image_url(:thumb).to_s %> | ||
| </p> | ||
| <%= f.submit "Submit", :class => "btn btn-lg btn-primary btn-block" %> | ||
| </div> | ||
|
|
||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
|
|
||
| <div class="page-header"> | ||
| <h1>Product List</h1> | ||
| </div> | ||
|
|
||
| <div class="span12"> | ||
| <div class="group"> | ||
| <%= link_to("New Product", new_admin_product_path , :class => "btn btn-mini btn-primary pull-right") %> | ||
| </div> | ||
| <table class="table"> | ||
| <thead> | ||
| <tr> | ||
| <td> # </td> | ||
| <td> 商品名稱 </td> | ||
| <td> 商品描述 </td> | ||
| <td> 商品數量 </td> | ||
| <td> 商品價格 </td> | ||
| <td> 商品縮圖 </td> | ||
| <td> 操作 </td> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <% @products.each do |product| %> | ||
| <tr> | ||
| <td> # </td> | ||
| <td> <%= link_to(product.title, admin_product_path(product)) %> </td> | ||
| <td> <%= product.description %> </td> | ||
| <td> <%= product.quantity %> </td> | ||
| <td> <%= product.price %> </td> | ||
| <td> | ||
| <%= image_tag product.image_url(:thumb).to_s %> | ||
| </td> | ||
| <td> <%= link_to("Edit", edit_admin_product_path(product), :class => "btn btn-mini")%> | ||
| <%= link_to("Delete", admin_product_path(product), :class => "btn btn-mini", :method=>:delete,:confirm=>"Are you sure?")%> | ||
| </td> | ||
| </tr> | ||
| <% end %> </tbody> | ||
| </table> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| <div class="page-header"> | ||
| <h1>Product New</h1> | ||
| </div> | ||
| <div class="container col-md-4 col-md-offset-1"> | ||
|
|
||
| <%= simple_form_for [:admin, @product],:html=> { role: 'form' } do |f| %> | ||
| <p> | ||
| <%= f.input :title, autofocus: true, label: '商品名稱', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :description, as: :text , label: '商品描述', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :quantity,autofocus: true, label: '商品數量', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :price, autofocus: true, label: '商品價格', input_html: { class: 'form-control', required: true } %> | ||
| </p> | ||
| <p> | ||
| <%= f.input :image, as: :file , label: '商品圖片' %> | ||
| </p> | ||
| <%= f.submit "Submit", :class => "btn btn-lg btn-primary btn-block" %> | ||
| </div> | ||
|
|
||
| <% end %> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這邊排版跑掉了