2020-08-02から1日間の記事一覧

コントローラーの書き方むずい ~~controller.rb ~~ def create Post.create(content: params[:content]) end (content: ↑カラム名 params[:content] ↑paramsとして送られてきたデータ

params コントローラーにたどり着くデータが格納されているハッシュオブジェクトのようなものです。フォームで送信されたデータもparamsの中に格納されてコントローラーで受け取られます。 フォームより送られたデータの場合、inputタグのnameに記載されたキ…

railsのフォーム <h1>新規投稿ページ</h1> <form action="/main" method="post"> <input type="text" name="content"> <input type="submit" value="投稿する"> </form> type 用途例 type="text" 1行テキストボックス type="password" パスワード入力ボックス(入力したテキストがアスタリスクなどに置き換えて表示される) type="checkbox" チェックボックス(複数選択可能) type="radio"…

routes.rb Rails.application.routes.draw do get 'posts', to: 'posts#index' get 'posts/new', to: 'posts#new' end 書いてから $ rails routes Prefix Verb URI Pattern Controller#Action posts GET /posts(.:format) posts#index posts_new GET /posts/…

routes.rb Rails.application.routes.draw do get 'posts', to: 'posts#index' get 'posts/new', to: 'posts#new' end 書いてから $ rails routes Prefix Verb URI Pattern Controller#Action posts GET /posts(.:format) posts#index posts_new GET /posts/…

HTTPメソッド どのような時に用いられるリクエストか GET ページを表示する操作のみを行う時 POST データを登録する操作をする時 PUT データを変更する操作をする時 DELETE データを削除する操作を行う時 アクション名 どのような時に動くか index 一覧表示…

erb注意点 <h1>トップページ</h1> <% @posts.each do |post| %> <%= post.content %> <%= post.created_at %> <% end %> %=タグは表示するときに使う %タグは表示しないときに使う eachの処理自体は表示しないのでeachの行は%タグでおk

Posts_controller.rb Class — def index @post = Post.find(1) end End Index.html.erb <%= @post.content %> contentはレコード名 変数@postの中(idの1)のcontentカラムのデータ表示してください Index.html.erb <%= @post.content %> <%= @post.created_a…

create データベースに登録するアクション @reshipe = Reshipe.new(reshipe_params) 空のモデルを@reshipeに代入 @resipe.user_id = current_user_id 誰が投稿したのか、user_idのカラムに今ログインしてる人のidつまり誰が投稿したのかが保持される @resh…

create データベースに登録するアクション @reshipe = Reshipe.new(reshipe_params) 空のモデルを@reshipeに代入 @resipe.user_id = current_user_id 誰が投稿したのか、user_idのカラムに今ログインしてる人のidつまり誰が投稿したのかが保持される @resh…