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 3 years have passed since last update.

Rails パラメータを取得する方法

Posted at

パラメータ(Parameter)

Railsアプリケーションは、フォームに記述した内容を送信した場合、それをParametersというハッシュ値に変換し、Railsアプリケーション内部へ送り込みます。

ハッシュ値(英:hash value)とは

ハッシュ関数から返される値であり

元のデータをあれやこれやして作られた一見すると適当に見える値です。
引用元

parameterを確認する

送られてきたパラメータはログから確認できます。
試しにブログを投稿し、ログを確認すると、下記のように表示されます。
4F03662C-9E19-483F-AB3C-14A60422F423_4_5005_c.jpeg

投稿したパラメータを確認できます。

パラメータを取得する

このパラメータを利用し、テーブルにレコードを新規追加します。

送られてきたパラメータを取得するためには、paramsメソッドを使用します。

paramsメソッドの使い方

blogs_controller.rb
class BlogsController < ApplicationController
  def index
  end

  def new
    @blog = Blog.new
  end

  def create
    Blog.create(title: params[:blog][:title], content: params[:blog][:content])
  end
end

このように、paramsメソッドを記述することで、送られてきたparameterをすべて取得できます。

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?