Guest User

Untitled

a guest
Jan 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. <%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %>
  2. <div>
  3. <%= f.label :name %>
  4. <%= f.text_field :name %>
  5.  
  6.  
  7. <%= f.label :description %>
  8. <%= f.text_area :description %>
  9.  
  10. <br>
  11.  
  12. <%=f.submit %>
  13. </div>
  14. <% end %>
  15.  
  16. class AlbumsController < ApplicationController
  17.  
  18. def index
  19. @user = User.find(params[:user_id])
  20. @albums = @user.albums.all
  21.  
  22. respond_to do |format|
  23. format.html
  24. format.json { render json: @albums }
  25. end
  26. end
  27.  
  28. def show
  29. @albums = Album.all
  30. @album = Album.find(params[:id])
  31. @photo = Photo.new
  32. end
  33.  
  34. def update
  35. end
  36.  
  37. def edit
  38. end
  39.  
  40. def create
  41. @user = User.find(params[:user_id])
  42. @album = @user.albums.build(params[:album])
  43. respond_to do |format|
  44. if @album.save
  45. format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
  46. format.json { render json: @album, status: :created, location: @album}
  47. else
  48. format.html { render action: "new" }
  49. format.json { render json: @album.errors, status: :unprocessable_entity }
  50. end
  51. end
  52. end
  53.  
  54. def new
  55. @user = User.find(params[:user_id])
  56. @album = Album.new
  57. end
  58.  
  59. def destroy
  60. end
  61.  
  62.  
  63. end
  64.  
  65. #album model
  66. class Album < ActiveRecord::Base
  67. attr_accessible :avatar, :name, :description
  68. has_many :user_albums
  69. has_many :users, :through => :user_albums
  70. has_many :photos
  71. end
  72.  
  73.  
  74. #user model
  75. class User < ActiveRecord::Base
  76.  
  77. has_secure_password
  78. attr_accessible :email, :name, :password, :password_confirmation
  79. validates_presence_of :password, :on => :create
  80.  
  81. validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
  82. validates_format_of :email, :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})Z/i, :on => :create
  83. validates_length_of :password, :minimum => 5, :on => :create
  84.  
  85. has_many :user_albums
  86. has_many :albums, :through => :user_albums
  87. accepts_nested_attributes_for :albums
  88.  
  89. end
  90.  
  91. #photo model (not up to there yet)
  92. class Photo < ActiveRecord::Base
  93. belongs_to :album
  94. end
  95.  
  96. Pholder::Application.routes.draw do
  97. resources :users do
  98. resources :albums
  99. end
  100.  
  101. resources :albums do
  102. resources :photos
  103. end
  104.  
  105. root :to => "users#index"
  106.  
  107. @album = @user.albums.build(params[:album])
  108.  
  109. @user.save
  110.  
  111. def create
  112. @user = User.find(params[:user_id])
  113. @album = @user.albums.build(params[:album])
  114. respond_to do |format|
  115. if @user.save
  116. print @album.errors
  117. format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
  118. format.json { render json: @album, status: :created, location: @album}
  119. else
  120. format.html { render action: "new" }
  121. format.json { render json: @album.errors, status: :unprocessable_entity }
  122. end
  123. end
  124. end
Add Comment
Please, Sign In to add comment