0
0

More than 1 year has passed since last update.

Rails 自分用

Last updated at Posted at 2022-12-21
1 / 2

controllerに処理を書く

controller.rb
def "html.erbとつなげる名前" ⇒ここに書いたものがhtml.erbとつながる(?)
    #処理を書く
end

#通常の処理
def 〇〇〇
    senaka = "タヌキのせなか"
   @html.erbで使う名前 = "タヌキのおなか"(変数など)
   @html.erbで使う名前2 = senaka
#html.erbで<%= @以下 %>で上記の文を出力できる
end

#select文
def □□□
    @html.erbで使う名前(html.erbと書く) = SQLの名前.all  全検索
                                  .find(params[:検索したいデータのメンバ名])  SQLなどのWHERE id = #{id} データ名で検索してそれに該当するカラムを取り出す。
end

#カラムを新規作成する際に呼ぶメソッド
def new
    @html.erb = SQLの名前.new

#新規作成文
def create
    @(html.erb) = @名前.new(データの名前) ⇒ 新しいカラム作成(?)
    if @(html.erb).save ⇒ 登録が保存、反映される(?)
    else
        render (templete):new, status: :unprocessable_entity
    end
end
=begin
→renderとは、HTTPレスポンスを作るためのメソッド。「erbファイル」を使ってHTMLなどのレスポンスを作る。
    #templete: 指定したいerbファイル名(今回ではnew.html.erbファイルを指定)
              "templete:"は省略できる

    status: エラーが起きた時、どのエラーが引き起こるかを設定することができる(?)
            今回ではunprocessable_entity 422のエラーが引き起こったとしている
参照:http://railman.net/railsguides/6.1/layouts_and_rendering.html
=end
#⇒新規作成ができる。


#update文
def update
    @(html.erb) = @名前.find(params[:データメンバ名])
       if  @(html.erb).update(データの名前, 上記newで作った名前)
     redirect_to @html.erbで使う名前
       else   render :new, status: :unprocessable_entity
    end
end
    #⇒ 更新(update)ができる。

#delete文
def destroy
    @book = Book.find(params[:データメンバ名])
    @book.destroy ⇒これで削除できる
    //[routes.rbで指定している('/名前')] 名前_pathでそのページにroutes.rbの設定しているページに飛ぶ
    redirect_to books_path
end

    private
        def データの名前
           #Aの名前を要求して、それでSQLからAが見つかった場合、データB, データCに介入することができる(?)
           params.require(:A).permit(:B, :C)
        end
end

routes.rbにURLの設定を書く

resource :SQLの名前
get⇒リソース(操作対象となるデータ)取得
例)LoginUserモデルなら
id, name, email
1 Yoshi Yoshi@example.com
これらのデータがリソースとなる。

get '/practice', to: 'home#practice', as: :practice
⇒URLに/practiceを入力すると'app/views/home/practice.erb'が表示されるようになる。

post⇒リソースへのデータ追加、子リソースの作成

put⇒リソースの全更新、作成

patch⇒リソースの部分更新

root ⇒全て共通のHTMLページ, 'OO#△△' で指定する
'OO#△△' = 'app/views/OO/△△.erb'である。

delete⇒リソースの削除

0
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
0
0