私は次のモデルを持っています:
User
has_many :armies
Army
belongs_to :user
追加された私のコントローラーcurrent_user
:
class ArmiesController < ApplicationController
before_filter :authenticate_user!
def new
@army = Army.new
end
def create
@army = current_user.armies.new(params[:army])
respond_to do |format|
if @army.save
format.html { redirect_to new_army_path, :notice => "New army added" }
else
format.html { render :new }
end
end
end
end
最後に作成したフォームの値を新しいフォームに使用したいと考えています。strength
例として私のフィールドを使用します。
<%= form_for @army do |f| %>
<%= f.label :strength, "Army Strength" %>
<%= f.text_field :amount %>
<%= f.submit "Create" %>
<% end %>
ユーザーがフィールドに入力した値を保存してstrength
、最後のフォームが作成された後もフォームに残るようにするにはどうすればよいですか?
編集:
def new
@army = Army.new(strength: session[:last_army_strength],
type_id: session[:last_type])
end
def create
@army = current_user.armies.new(params[:army])
session[:last_army_strength] = params[:army][:strength]
session[:last_type] = params[:army][:type_id]
respond_to do |format|
if @army.save
format.html { redirect_to new_army_path, :notice => "New army added" }
else
format.html { render :new }
end
end
end
end