LoginSignup
12
6

More than 5 years have passed since last update.

Ruby on Rails .erbテンプレートにてform_withのフォームに初期値をセットする

Last updated at Posted at 2019-03-26
main_controller.rb
@title = "default form value"

Controllerでは、↑こんな感じで、インスタンス変数をセットしてViewに渡す。

main.erb
<%= form_with scope: :post,:url => {:action => :update} do |form| %>
    <%= form.text_field :title, value: @title %>
    <%= form.button '送信', id: 'customButton', type: 'button', onclick: 'submit()' %>
<% end %>

Viewでは、↑こんな感じで、Controllerから受け取ったインスタンス変数を value: @hoge というふうにフォームにセットする。

12
6
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
12
6