| | | |
| # Various Comments | | # Various Comments |
| | | |
| # Code Examples | | # Code Examples |
| | | |
t | | t | ## Migration file
|
| | |
|
| | | class Bar < ActiveRecord::Migration
|
| | | def self.up
|
| | | create_table :bars do |table|
|
| | | table.column :name, :string
|
| | | table.column :location, :text
|
| | | table.column :wifi, :integer # 1 |
| | | - Yes, 2 - No, 3 - Paid
|
| | | table.column :creationdate, :timestamp
|
| | | table.column :editdate, :timestamp
|
| | | end
|
| | | end
|
| | |
|
| | | def self.down
|
| | | drop_table :bars
|
| | | end
|
| | | end
|
| | |
|
| | | ## Model
|
| | |
|
| | | class Bar < ActiveRecord::Base
|
| | | has_and_belongs_to_many :beers
|
| | | belongs_to :user
|
| | |
|
| | | has_many :comments
|
| | |
|
| | | validates_presence_of :name
|
| | |
|
| | | def wifi_descr
|
| | | return "Yes" if wifi == 1
|
| | | return "No" if wifi == 2
|
| | | return "Paid" if wifi == 3
|
| | | end
|
| | |
|
| | | end
|
| | |
|
| | | ## Controller
|
| | |
|
| | | i show new/create here.. the exact same looks edit |
| | | /update
|
| | |
|
| | | def new
|
| | | @bar = Bar.new
|
| | | @beers = Beer.find_all
|
| | | end
|
| | |
|
| | | def create
|
| | | @bar = Bar.new(params[:bar])
|
| | | @bar.editdate = Time.now
|
| | | @bar.beers = Beer.find(params[:beer_ids]) |
| | | if params[:beer_ids]
|
| | | @bar.user_id = current_user.id
|
| | | if @bar.save
|
| | | flash[:notice] = 'Bar was successfully c |
| | | reated.'
|
| | | redirect_to :action => 'list'
|
| | | else
|
| | | render :action => 'new'
|
| | | end
|
| | | end
|
| | |
|
| | | ## Template (View)
|
| | |
|
| | | this is the \_forms.rhtml template which is used t |
| | | o edit and create bars. it was 90% autogenerated
|
| | |
|
| | | <%= error_messages_for 'bar' %>
|
| | |
|
| | | <!--[form:bar]-->
|
| | | <p><label for="bar_name">Name</label><br/>
|
| | | <%= text_field 'bar', 'name' %></p>
|
| | |
|
| | | <p><label for="bar_location">Location</label>< |
| | | br/>
|
| | | <%= text_area 'bar', 'location' %></p>
|
| | |
|
| | | <p><label for="bar_beers">Beers</label><br/>
|
| | | <%= select 'bar', 'beer_ids', Beers.find_all.c |
| | | ollect { |b| [ b.name, b.id ] }, { }, { :multiple |
| | | => 'multiple' } %>
|
| | |
|
| | | <p><label for="bar_wifi">Wifi</label><br/>
|
| | | <%= select 'bar', 'wifi', { 'Yes' => 1, 'No' = |
| | | > 2, 'Paid' => 3 } %></p>
|
| | | <!--[eoform:bar]-->
|
| | |
|
| | |
|
| # Conclusion | | # Conclusion |