learn from codecademy
rails new myapp
bundle install
rails generate controller mycontroller
rails generate model mymodl
rake db:migrate updates the database with the new messages data model
rake db:seed seeds the database with sample data from db/seeds.rb
Ruby on Rails defines seven standard controller actions can be used to do common things such as display and modify data.
If you want to create routes for all seven actions in your app, you can add aresource route to your routes file. This resource route below maps URLs to the Messages controller's seven actions (index
, show
, new
, create
, edit
,update
, and destroy
):
resources :messages
If you only want to create routes for specific actions, you can use :only
to fine
tune the resource route. This line maps URLs only to the Message controller'sindex
and show
actions.
resources :messages, only: [:index, :show]
-
has_many :destinations
denotes that a single Tag can have multiple Destinations. -
belongs_to :tag
denotes that each Destination belongs to a single Tag.
Open the migration file in db/migrate/ for the tags table, and add the following columns:
- a
string
column calledtitle
- a
string
column calledimage
Next in the migration file for the destinations table, add the following columns:
- a
string
column calledname
- a
string
column calledimage
- a
string
column calleddescription
- the line
t.references :tag
@tag = Tag.find(params[:id])
@destinations = @tag.destinations
end
@destinations = @tag.destinations
retrieves all the destinations that belong to the tag, and stores them in
@destinations
def update
@destination = Destination.find(params[:id])
if @destination.update_attributes(destination_params)
redirect_to(:action => 'show', :id => @destination.id)
else
render "edit"
end
end
private
def destination_params
params.require(:destination).permit(:name, :description)
end