Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. class ProductsController < ApplicationController
  2. before_action :signed_in_user,only:[:new,:create]
  3. before_action :find_user_object,except:[:create]
  4.  
  5. def index
  6. @products = @user.products.all
  7. end
  8.  
  9. def show
  10. @product = @user.product.build.find(params[:id])
  11. end
  12.  
  13. def new
  14. @user = User.find(params[:user_id])
  15. # => 多分before action化させる方が良い
  16. #urlでproducts/newなっててUserのidが取れてない。
  17. redirect_to signin_url, notice:"U have to sign in to publish your furniture." unless sign_in @user
  18. @product = Product.new
  19. end
  20.  
  21. def create
  22. @product = @user.products.build(products_params)
  23. if @product.save
  24. flash[:success] = "You could add new item:)"
  25. redirect_to @user #後にaction: :indexに変更したい
  26. else
  27. flash.now[:error] = "You couldn't add an item."
  28. render 'new'
  29. end
  30. end
  31.  
  32. def edit
  33. end
  34.  
  35. def update
  36. if @product.update_attributes(products_params)
  37. flash[:success] = "You updated your product info"
  38. redirect_to @products
  39. else
  40. flash.now[:error] = "couldn't update :("
  41. redirect_to products_edit_path
  42. end
  43. end
  44.  
  45. def destroy
  46. #あった方がいいかもしれない@user = Product.find(params[:id])
  47. @product.destroy
  48. redirect_to root_url
  49. end
  50.  
  51.  
  52.  
  53.  
  54. private
  55.  
  56. def products_params
  57. params.require(:product).permit(:id,:name,:kind,:size,:discription,:price)
  58. end
  59.  
  60.  
  61. #before_action
  62. def signed_in_user
  63. redirect_to signin_url, notice:"Please sign in." if signed_in?
  64. end
  65.  
  66. def find_user_object
  67. @user = User.find_by(params[:user_id])
  68. end
  69.  
  70. end
  71.  
  72. class UsersController < ApplicationController
  73. include UsersHelper
  74. before_action :signed_in_user,only:[:edit,:update]
  75. before_action :correct_user,only:[:edit,:update]
  76.  
  77. def show
  78. @user = User.find(params[:id])
  79. sign_in @user if signed_in?
  80. end
  81.  
  82. def new
  83. @user = User.new
  84. end
  85.  
  86. def create
  87. @user = User.new(user_params)
  88. if @user.save!
  89. sign_in @user
  90. flash[:success] = "Success creating user"
  91. redirect_to @user
  92. else
  93. flash.now[:error] = "couldn't create...."
  94. render 'new'
  95. end
  96. end
  97.  
  98. def edit
  99. end
  100.  
  101. def update
  102. if @user.update_attributes(user_params)
  103. flash[:success] = "User info was updated"
  104. redirect_to user_url
  105. else
  106. flash.now[:error] = "You could not edit your profile"
  107. render 'edit'
  108. end
  109. end
  110.  
  111. def destroy
  112. #あった方がいいかもしれない@user = Product.find(params[:id])
  113. @user.destroy
  114. redirect_to root_url
  115. end
  116.  
  117. private
  118. def user_params
  119. params.require(:user).permit(:id,:user_id,:name,:email,:username,:status,:from,:when,:password,:password_confirmation)
  120. end
  121.  
  122. #before_aciton
  123. def correct_user
  124. @user = User.find(params[:id])
  125. redirect_to signin_url,notice:"You have to sign in to edit your profile." unless current_user=(@user)
  126. end
  127.  
  128. def signed_in_user
  129. redirect_to signin_url, notice:"Please sign in." if signed_in?
  130. end
  131.  
  132. end
  133.  
  134. KaguShop::Application.routes.draw do
  135. resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do
  136. resources :products,only:[:index,:new,:create,:destroy,:show,:new,:edit,:update]
  137. end
  138.  
  139. resources :sessions,only:[:new,:create,:destroy]
  140. resources :carts,only:[:new,:create,:destroy]#,:showに関しては恐らくいらない。newで既にオブジェクトも作る
  141.  
  142.  
  143. root 'products#index'
  144. match '/signup', to:'users#new',via:'get'
  145. match '/signin', to:'sessions#new', via:'get'
  146. match '/signout', to:'sessions#destroy', via:'delete'
  147. match '/contact', to:'nomal_pages#contact', via:'get'
  148.  
  149. # The priority is based upon order of creation: first created -> highest priority.
  150. # See how all your routes lay out with "rake routes".
  151.  
  152. # You can have the root of your site routed with "root"
  153. # root 'welcome#index'
  154.  
  155. # Example of regular route:
  156. # get 'products/:id' => 'catalog#view'
  157.  
  158. # Example of named route that can be invoked with purchase_url(id: product.id)
  159. # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
  160.  
  161. # Example resource route (maps HTTP verbs to controller actions automatically):
  162. # resources :products
  163.  
  164. # Example resource route with options:
  165. # resources :products do
  166. # member do
  167. # get 'short'
  168. # post 'toggle'
  169. # end
  170. #
  171. # collection do
  172. # get 'sold'
  173. # end
  174. # end
  175.  
  176. # Example resource route with sub-resources:
  177. # resources :products do
  178. # resources :comments, :sales
  179. # resource :seller
  180. # end
  181.  
  182. # Example resource route with more complex sub-resources:
  183. # resources :products do
  184. # resources :comments
  185. # resources :sales do
  186. # get 'recent', on: :collection
  187. # end
  188. # end
  189.  
  190. # Example resource route with concerns:
  191. # concern :toggleable do
  192. # post 'toggle'
  193. # end
  194. # resources :posts, concerns: :toggleable
  195. # resources :photos, concerns: :toggleable
  196.  
  197. # Example resource route within a namespace:
  198. # namespace :admin do
  199. # # Directs /admin/products/* to Admin::ProductsController
  200. # # (app/controllers/admin/products_controller.rb)
  201. # resources :products
  202. # end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement