日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rails 命令

發布時間:2024/7/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rails 命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建新項目:rails new meetup -d mysql

migrate數據庫:rake db:create db:migrate

生成controller:rails generate controller welcome

生成model:rails g model event name:string description:text is_public:boolean capacity:integer

查看路由:?rake routes

rake幫助查看:rake -T

scaffold生成簡易crud程序命令:rails g scaffold person name:string bio:text birthday:date

生成新的欄位:rails g migration add_status_to_events

行數統計:rake stats

查看log:tail -f log/development.log

?

一對多:

class Category < ActiveRecord::Basehas_many :events endclass Event < ActiveRecord::Basebelongs_to :category# ... end

創建、刪除、查找、
u = User.first
a = Article.new(...........)
a.user_id = u.id
a.save e = Event.first e.attendees.destroy_all # 一筆一筆刪除 e 的 attendee,並觸發 attendee 的 destroy 回呼 e.attendees.delete_all # 一次砍掉 e 的所有 attendees,不會觸發個別 attendee 的 destroy 回呼

a = e.attendees.find(3) attendees = e.attendees.where( :name => 'ihower' ) #查出來的是數組

一對一: class Event < ActiveRecord::Basehas_one :location # 單數#... endclass Location < ActiveRecord::Basebelongs_to :event # 單數 end

多對多 class Event < ActiveRecord::Basehas_many :event_groupshipshas_many :groups, :through => :event_groupships endclass EventGroupship < ActiveRecord::Basebelongs_to :eventbelongs_to :group endclass Group < ActiveRecord::Basehas_many :event_groupshipshas_many :events, :through => :event_groupships end 一對多創建 @attendee = @event.attendees.build
一對多路由: resources :events doresources :attendees, :controller => 'event_attendees' end 一對一創建 @location = @event.build_location
一對一路由

 ?resources :user do
  resource :role, :controller => 'user_role'
 end

Nested Model

user的model里加上 ? ?accepts_nested_attributes_for :role, :allow_destroy => true, :reject_if => :all_blank

user 的new.html里加上

<%= f.fields_for :role do |role_form| %>
<p>
<%= role_form.label :name, "Role Name" %>
<%= role_form.text_field :name %>

<% unless role_form.object.new_record? %>
<%= role_form.label :_destroy, 'Remove:' %>
<%= role_form.check_box :_destroy %>
<% end %>
</p>
<% end %>

user的helper加上

def setup_user(user)
  user.build_role unless user.role
  user
end

user的controller改成

def event_paramsparams.require(:event).permit(:name, :description, :category_id, :location_attributes => [:name] ) end

提交表單改成 <%= form_for setup_event(@event), :url => events_path do |f| %> ?


?

轉載于:https://www.cnblogs.com/Akishimo/p/4360303.html

總結

以上是生活随笔為你收集整理的rails 命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。