Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %>
- <div>
- <%= f.label :name %>
- <%= f.text_field :name %>
- <%= f.label :description %>
- <%= f.text_area :description %>
- <br>
- <%=f.submit %>
- </div>
- <% end %>
- class AlbumsController < ApplicationController
- def index
- @user = User.find(params[:user_id])
- @albums = @user.albums.all
- respond_to do |format|
- format.html
- format.json { render json: @albums }
- end
- end
- def show
- @albums = Album.all
- @album = Album.find(params[:id])
- @photo = Photo.new
- end
- def update
- end
- def edit
- end
- def create
- @user = User.find(params[:user_id])
- @album = @user.albums.build(params[:album])
- respond_to do |format|
- if @album.save
- format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
- format.json { render json: @album, status: :created, location: @album}
- else
- format.html { render action: "new" }
- format.json { render json: @album.errors, status: :unprocessable_entity }
- end
- end
- end
- def new
- @user = User.find(params[:user_id])
- @album = Album.new
- end
- def destroy
- end
- end
- #album model
- class Album < ActiveRecord::Base
- attr_accessible :avatar, :name, :description
- has_many :user_albums
- has_many :users, :through => :user_albums
- has_many :photos
- end
- #user model
- class User < ActiveRecord::Base
- has_secure_password
- attr_accessible :email, :name, :password, :password_confirmation
- validates_presence_of :password, :on => :create
- validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
- validates_format_of :email, :with => /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})Z/i, :on => :create
- validates_length_of :password, :minimum => 5, :on => :create
- has_many :user_albums
- has_many :albums, :through => :user_albums
- accepts_nested_attributes_for :albums
- end
- #photo model (not up to there yet)
- class Photo < ActiveRecord::Base
- belongs_to :album
- end
- Pholder::Application.routes.draw do
- resources :users do
- resources :albums
- end
- resources :albums do
- resources :photos
- end
- root :to => "users#index"
- @album = @user.albums.build(params[:album])
- @user.save
- def create
- @user = User.find(params[:user_id])
- @album = @user.albums.build(params[:album])
- respond_to do |format|
- if @user.save
- print @album.errors
- format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
- format.json { render json: @album, status: :created, location: @album}
- else
- format.html { render action: "new" }
- format.json { render json: @album.errors, status: :unprocessable_entity }
- end
- end
- end
Add Comment
Please, Sign In to add comment