1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Udemyのコース「【作りながら覚える!】 Ruby on Rails を用いたウェブ開発(Airbnbを作ろう!) 」を学ぶ。part 4-1〜6

Posted at

Udemyのこちらのコースを受けながら、疑問に思ったこと、調べた結果、大事なことをまとめていきます。

part4-1 リスティングモデルの作成

rails g model Listing home_type:string pet_type:string pet_size:string breeding_years:integer address:string listing_title:string listing_content:text price_pernight:integer active:boolean user:references

rails g model はテーブルを新たに作成する。
rails g migration は今あるテーブルにカラムを追加する感じ。

最後に書いた user:references のおかげでuser_idカラムも追加されている。他のテーブルとの結びつけだ。

これを行うとlisting.rbができ、中身はこんなん。userに所属しているって書いてある。これはuser:referencesを書いたから。

class Listing < ApplicationRecord
  belongs_to :user
end

userとlistingの関係。

userは複数のlistingを持てる。なので、

hasmany :listings

をuser.rbに追記。モデルね。

必須項目の設定

  validates :home_type, presence:true

モデルファイルに書くことで必須にできる。

自己課題

  • 上記のlistingモデルの作成
  • validateの設定

part4-2-1 リスティングコントローラーの作成1

rails g controller Listings index show new create edit update

resource :listings
とすると、routesを自動生成してくれる。

resources :pages

pages GET /pages(.:format) pages#index

POST /pages(.:format) pages#create

new_page GET /pages/new(.:format) pages#new

edit_page GET /pages/:id/edit(.:format) pages#edit

page GET /pages/:id(.:format) pages#show

PUT /pages/:id(.:format) pages#update

DELETE /pages/:id(.:format) pages#destroy

ログインしていないと、シッターになれないようにする。

listingコントローラーに
before_action :authenticate_user!

と書くと、ログインしていないと次に行けなくなります。

自己課題

  • リスティングコントローラーの作成
    -navbarにシッターになるを追加。(ログインしているとき。)

part4-2-2 リスティングコントローラーの作成2

自己課題

  • ログインページの重複を消す。facebookとmesseage

part4-2-3 リスティングnewページを整える

自己課題

part4-3-1 リスティングcreate

listingコントローラーに追記。

def new
  #現在のユーザーもlistingの作成
  @listing = current_user.listings.build
end

def create
  #パラメーターととと現在のユーザーのlistingを作成
  @listing = current_user.listings.build(listing_params)

  if @listing.save
    redirect_to edit_listing_path(@listing), notice: "リスティングを作成・保存しました。"
  else
    redirect_to new_listing_path, notice: "リスティングを作成・保存できませんでした。"
  end
end

def edit
end

def update
end

private
def listing_params
  params.require(:listing).permit(:home_type, :pet_type, :breeding_years, :pet_size)
end

自己課題

  • シッター情報を入力し、保存できるようにする。

part4-3-2 createした後に飛ぶページを変更する

もっと新たな情報を入力するために新しいページを作成する。

path manage-listing_basics
URL manage-listing/:id/basics
action listings#basics

のページを作成しようと思ったら、以下のコードをroutes.rbに記入。

get 'manage-listing/:id/basics' => 'listings#basics', as: 'manage-listing_basics'

自己課題

- routesとviewを作成

part4-4 リスティング編集ページ(basics)

自己課題

part4-5-1 リスティング編集ページ(リスティング内

自己課題

  • descriptionアクションを追加。ルートもね。

part4-5-2 リスティング編集ページ続き

controller内で共通の変数をもつ場合。

private の下に適当に関数を定義。set_listingとしたら、

before_action :set_listing, only:[:basics, :description, :address, :price, :photos, :calender, :bankaccount, :publish]

以下のコードを上に書くことによりまとめることができる。

自己課題

  • すべてのactionに対し、ルート、verticalnavberにリンク。

part4-6 verticalナブバーのactive

表示しているページがnavbar上でわかるようにしたい。

この記述をhelpers/application_helper.rb加える。

helperに関数をかけば、全てのページで呼び出せるようになる。

application_helper.rb

	module ApplicationHelper
		def controller?(controller) #controller?という関数名でcontrollerという引数を持っている。
			controller.include?(params[:controller]) #引数のcontrollerの中に、
# 現在のコントローラー(後ろのコントローラー :controllerのこと)が含まれていたら、True.
		end

		def action?(action)
			action.include?(params[:action])
		end
	end

vertivalnavbarの一つ一つの要素にこれを書く。

application_helper.rb
<li class = "<%= 'active' if controller?("listings") && action?("basics") %>" >
    <%= link_to"基本事項", manage_listing_basics_path(@listing) %>
</li>

controller?とactionがTureならば、activeが追加される。

参照

add class nav active stackflow railsとかでググれば出てくる。ググり力大事。

自己課題

  • verticalnavbarに、見ているページにactiveクラスを追加。
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?