您的位置:首页 > 编程语言 > Ruby

ruby on rails 随记

2017-01-14 14:23 162 查看
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's
index
 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:

string
 column
called 
title


string
 column
called 
image


Next in the migration file for the destinations table, add the following columns:

string
 column
called 
name


string
 column
called 
image


string
 column
called 
description


the line 
t.references
:tag


def show

    @tag = Tag.find(params[:id])

    @destinations = @tag.destinations

  end

The 
@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: