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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

rails 命令

發(fā)布時(shí)間:2024/7/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rails 命令 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

創(chuàng)建新項(xiàng)目:rails new meetup -d mysql

migrate數(shù)據(jù)庫(kù):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生成簡(jiǎn)易crud程序命令:rails g scaffold person name:string bio:text birthday:date

生成新的欄位:rails g migration add_status_to_events

行數(shù)統(tǒng)計(jì):rake stats

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

?

一對(duì)多:

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

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

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

一對(duì)一: class Event < ActiveRecord::Basehas_one :location # 單數(shù)#... endclass Location < ActiveRecord::Basebelongs_to :event # 單數(shù) end

多對(duì)多 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 一對(duì)多創(chuàng)建 @attendee = @event.attendees.build
一對(duì)多路由: resources :events doresources :attendees, :controller => 'event_attendees' end 一對(duì)一創(chuàng)建 @location = @event.build_location
一對(duì)一路由

 ?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| %> ?


?

轉(zhuǎn)載于:https://www.cnblogs.com/Akishimo/p/4360303.html

總結(jié)

以上是生活随笔為你收集整理的rails 命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。