Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ProductsController < ApplicationController
- before_action :signed_in_user,only:[:new,:create]
- before_action :find_user_object,except:[:create]
- def index
- @products = @user.products.all
- end
- def show
- @product = @user.product.build.find(params[:id])
- end
- def new
- @user = User.find(params[:user_id])
- # => 多分before action化させる方が良い
- #urlでproducts/newなっててUserのidが取れてない。
- redirect_to signin_url, notice:"U have to sign in to publish your furniture." unless sign_in @user
- @product = Product.new
- end
- def create
- @product = @user.products.build(products_params)
- if @product.save
- flash[:success] = "You could add new item:)"
- redirect_to @user #後にaction: :indexに変更したい
- else
- flash.now[:error] = "You couldn't add an item."
- render 'new'
- end
- end
- def edit
- end
- def update
- if @product.update_attributes(products_params)
- flash[:success] = "You updated your product info"
- redirect_to @products
- else
- flash.now[:error] = "couldn't update :("
- redirect_to products_edit_path
- end
- end
- def destroy
- #あった方がいいかもしれない@user = Product.find(params[:id])
- @product.destroy
- redirect_to root_url
- end
- private
- def products_params
- params.require(:product).permit(:id,:name,:kind,:size,:discription,:price)
- end
- #before_action
- def signed_in_user
- redirect_to signin_url, notice:"Please sign in." if signed_in?
- end
- def find_user_object
- @user = User.find_by(params[:user_id])
- end
- end
- class UsersController < ApplicationController
- include UsersHelper
- before_action :signed_in_user,only:[:edit,:update]
- before_action :correct_user,only:[:edit,:update]
- def show
- @user = User.find(params[:id])
- sign_in @user if signed_in?
- end
- def new
- @user = User.new
- end
- def create
- @user = User.new(user_params)
- if @user.save!
- sign_in @user
- flash[:success] = "Success creating user"
- redirect_to @user
- else
- flash.now[:error] = "couldn't create...."
- render 'new'
- end
- end
- def edit
- end
- def update
- if @user.update_attributes(user_params)
- flash[:success] = "User info was updated"
- redirect_to user_url
- else
- flash.now[:error] = "You could not edit your profile"
- render 'edit'
- end
- end
- def destroy
- #あった方がいいかもしれない@user = Product.find(params[:id])
- @user.destroy
- redirect_to root_url
- end
- private
- def user_params
- params.require(:user).permit(:id,:user_id,:name,:email,:username,:status,:from,:when,:password,:password_confirmation)
- end
- #before_aciton
- def correct_user
- @user = User.find(params[:id])
- redirect_to signin_url,notice:"You have to sign in to edit your profile." unless current_user=(@user)
- end
- def signed_in_user
- redirect_to signin_url, notice:"Please sign in." if signed_in?
- end
- end
- KaguShop::Application.routes.draw do
- resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do
- resources :products,only:[:index,:new,:create,:destroy,:show,:new,:edit,:update]
- end
- resources :sessions,only:[:new,:create,:destroy]
- resources :carts,only:[:new,:create,:destroy]#,:showに関しては恐らくいらない。newで既にオブジェクトも作る
- root 'products#index'
- match '/signup', to:'users#new',via:'get'
- match '/signin', to:'sessions#new', via:'get'
- match '/signout', to:'sessions#destroy', via:'delete'
- match '/contact', to:'nomal_pages#contact', via:'get'
- # The priority is based upon order of creation: first created -> highest priority.
- # See how all your routes lay out with "rake routes".
- # You can have the root of your site routed with "root"
- # root 'welcome#index'
- # Example of regular route:
- # get 'products/:id' => 'catalog#view'
- # Example of named route that can be invoked with purchase_url(id: product.id)
- # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
- # Example resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
- # Example resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
- # Example resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
- # Example resource route with more complex sub-resources:
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', on: :collection
- # end
- # end
- # Example resource route with concerns:
- # concern :toggleable do
- # post 'toggle'
- # end
- # resources :posts, concerns: :toggleable
- # resources :photos, concerns: :toggleable
- # Example resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement