LoginSignup
0
0

More than 5 years have passed since last update.

備忘録 パスやらハッシュやら etc. の記載について

Last updated at Posted at 2017-03-21

:user     #シンボル, アクション

[routes.rb]

  • resources :photos, :books, :videos  #シンボル
  • resources :users, only: [:show]  #usersシンボルと showアクション
  • resources :strong>notes, only: [:show, :create, :edit, :update, :destroy] do  
  • /unlike/:note_id

/models/[user.rb]

  • belongs_to :user  #シンボル
  • has_many :like_notes, through: :likes, source: :note  #シンボル

[xxx_controller.rb]

  • :user_id  #パス
  • before_action :set_user, only: [:show, :edit, :update, :like_notes]  #シンボル
  • @project = Project.find(params[:id])
     #URLで飛んできた数値は paramsで取得できる。
     []内は、シンボル含め rake routes → URI Patternにある記載通り。
  • params[:project].permit(:title)
     「 paramsの中の projectで渡ってきた中の物のうち titleだけを取得。」

[201703xxx_xxx_xxx.rb]

  • t.integer :user_id  #シンボル
  • params.permit(:text, :tweet_id)  #シンボル

[xxx.rb]

  • data: { turbolinks: { track: true }}
     シンボル型のキーにハイフンが入っているとまずい。
     X : data-turbolinks-track: true

:user =>  , user:    #データ型, ハッシュ, キー

  • [vagrant@localhost myapp]$ rails generate scaffold User name:string score:integer
     #型指定する scaffoldコマンド。

[xxx.rb]

  • langueges = { English: "5億1000万", Russian: 180000000, 日本語: 125000000 }

  • p langueges[:English]   #値の呼出
  • langueges[:English] = 510000000   #値の書換
  • p langueges.size   #キーと値のセットの数
  • p langueges.keys   #全キー呼出
  • p langueges.values   #全値の呼出
  • p langueges.has_key?(:English)   #キー存在確認

  • langueges = {:English => "5億1000万", :Russian => 180000000, :日本語 => 125000000 }
  • langueges = { "English" => "5億1000万", "Russian" => 180000000, "日本語" => 125000000 }

  • Tweet.where(user_id: user.id))   #association定義後のハッシュ

[routes.rb]

  • delete /unlike/:note_id => 'likes#unlike', as: 'unlike'
     #URL(DELETE /xxxs/:note_id) を likesコントローラの unlikeアクションに割振り、params{note_id: ○} を生成。

[unlike.js.erb]

  • $('#like-link').html('<%= escape_javascript(render("likes/like_links", note: @note)) %>');
     #変数note に @note を渡す。

[ViewController.swift]

  • let text: String = textField.text!

_, . の表現

[likes_controller.rb]

  • current_user.likes.build(note_id: note.id)
     note_id はカラム名。ログインユーザーに紐付いた likesテーブルから likeインスタンスを取得。
  • current_user.id   #ログイン中のユーザーの id
  • article_path(article)  #アクションへのリンク、()内は id
  • like.save  #Likeインスタンスを保存

[index.html.erb]

  • <% = link_to '詳細', "/tweets/# => {tweet.id}", method: :get %>
     '詳細'ボタンを getとし、該当idからデータを表示。
  • <%= link_to '@note.likes.count', liking_users_note_path(@note.id) %>
     送信先 URL(rake routes →Prefix+_pathURI Patternに変換)の引数に @noteの idを指定。
  • <%= link_to "[Edit]", edit_project_path(project.id), style: "font-family:cursive;" %>
     送信先URL edit_project_path の引数に projectの idを指定。

[ _note.html.erb]

  • <%= note.likes.count %>人が〜
      #投稿一覧画面。投稿がユーザーにいいね!されている数。インスタンス変数名に@なし。

@note     #インスタンス変数, パーシャル呼出

[xxx_controller.rb]

  • @task = @project.tasks.create(task_params) 
      #project情報をもとにtaskを作る。
  • @users = @note.liking_users 
      #@noteのゲッターを用いてliking_usersを取得し、変数@usersに代入。

[xxx.html.erb]

  • @note
      #note_path(@note)と解釈される。note_path の省略形。
  • @user.notes
      #あるユーザー@userが作成した全投稿 (Note)を取得。
  • <%= render @users %>
      #パーシャルファイル _user.html.erb 呼出。
  • note_path(@note)  #=> note_path(@note.id)
  • <%= @user.likes.count %>個の投稿に 〜
      #ユーザーが投稿をいいね!している数
  • <%= @note.likes.count %>人が〜
      #投稿詳細画面  投稿がユーザーにいいね!されている数
  • <%= link_to 'いいね!を取り消す', unlike_path(@note.id), method: :delete %>
      #アクションへのリンク、()内は id
  • like = current_user.likes.find_by(note_id: @note.id)
      #ログインユーザーに紐付いた likesテーブルから likeインスタンスを取得。

ネストした連想配列から呼出す書き方

<?php
$menus = array(
 array('name' => 'CURRY', 'price' => 900),   #arrayを$menuにするとエラー
 array('name' => 'PASTA', 'price' => 1200),
 array('name' => 'COFFEE', 'price' => 600)
);
foreach ($menus as $menu) {
 echo $menu['name'].'は'.$menu['price'].'円です'.'<br>';
}
?>

xxx.rb
"." = "/"    #=> ディレクトリ指示子
routes.rb
  get "/" => "projects#index"
  root "projects#index"    #=> 上行と同じ意味。
rake_routes
(.:format)    #=> formatオプション html形式やjson形式等複数の形式で出力できる

 学習過程で随時更新。

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