ruby on rails 随记

本文介绍如何使用Ruby on Rails创建应用程序,包括初始化项目、生成控制器和模型、迁移数据库等步骤,并详细解释了资源路由的配置方法及数据库关联设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Seven actions

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 (indexshownewcreateedit,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:

  • 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值