2つのモデルのイベントとタスクがあります。Ruby on Railsの更新モデルで、複数の行が1つの形式で表示される
イベントには多くのタスクがあります。タスクは イベントでネストされたリソースなので、最初にイベントを作成して、そこに作成したいタスクの数をユーザに尋ねます。
私はイベントを作成し、ユーザーは3つのタスクを作成したいとしましょう。私はイベントの作成に成功した後、1
を2つの段階でそれを行うとしないようにしたい、今私は3タスク名フィールドを持つようにしたいここ
新しい/イベント/ 1 /タスク/に移動したときに、ユーザーをそれを提出し、私はここに、この
を達成するにはどうすればよいのイベント1
に対してタスクテーブルで作成された3行がなければならないことは
<%= form_for [@event, @task] do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_form.html.erb作業ですタスクコントローラ
def new
@event=Event.find(params[:event_id])
@event.task_count do
@choice = @event.tasks.build
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @task }
end
end
# POST /tasks
# POST /tasks.json
def create
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render json: @task, status: :created, location: @task }
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end