railsを使い簡単なアプリの開発を進めているのですが、「Form」に入力したデータを反映させるにはどの様な操作を行えば反映させる事が出来ますか??
viewの設定を行い項目名を表示する事は出来たのですが、入力したデータが反映されず空欄になってしまうので入力データを出力する設定の仕方を教えて頂きたいです。
回答よろしくお願いします。
「_form.html.erb」
<% if syotum.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(syotum.errors.count, "error") %> prohibited this syotum from being saved:</h2> <ul> <% syotum.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= form.label :from %> <%= form.text_field :from, id: :syotum_from %> </div> <div class="field"> <%= form.label :year %> <%= form.number_field :year, id: :syotum_year %> </div> <div class="field"> <%= form.label :school %> <%= form.text_area :school, id: :syotum_school %> <div class="field"> <%= form.label :seibetU %> <%= form.text_field :sex, id: :syotum_sex %> <div class="actions"> <%= form.submit %> </div> <% end %>
「index.html.erb」
<p id="notice"><%= notice %></p> <h1>Syota</h1> <table> <thead> <tr> <th>From</th> <th>Year</th> <th>School</th> <th>Seibetu</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @syota.each do |syotum| %> <tr> <td><%= syotum.from %></td> <td><%= syotum.year %></td> <td><%= syotum.school %></td> <td><%= syotum.sex %></td> <td><%= link_to 'Show', syotum %></td> <td><%= link_to 'Edit', edit_syotum_path(syotum) %></td> <td><%= link_to 'Destroy', syotum, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'New Syotum', new_syotum_path %> ``` 「syota_controller.rb」
class SyotaController < ApplicationController
before_action :set_syotum, only: [:show, :edit, :update, :destroy]
GET /syota
GET /syota.json
def index
@syota = Syotum.all
end
GET /syota/1
GET /syota/1.json
def show
end
GET /syota/new
def new
@syotum = Syotum.new
end
GET /syota/1/edit
def edit
end
POST /syota
POST /syota.json
def create
@syotum = Syotum.new(syotum_params)
respond_to do |format| if @syotum.save format.html { redirect_to @syotum, notice: 'Syotum was successfully created.' } format.json { render :show, status: :created, location: @syotum } else format.html { render :new } format.json { render json: @syotum.errors, status: :unprocessable_entity } end end
end
PATCH/PUT /syota/1
PATCH/PUT /syota/1.json
def update
respond_to do |format|
if @syotum.update(syotum_params)
format.html { redirect_to @syotum, notice: 'Syotum was successfully updated.' }
format.json { render :show, status: :ok, location: @syotum }
else
format.html { render :edit }
format.json { render json: @syotum.errors, status: :unprocessable_entity }
end
end
end
DELETE /syota/1
DELETE /syota/1.json
def destroy
@syotum.destroy
respond_to do |format|
format.html { redirect_to syota_url, notice: 'Syotum was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_syotum
@syotum = Syotum.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through. def syotum_params params.require(:syotum).permit(:from, :year) end
end
回答1件
あなたの回答
tips
プレビュー