我正在尝试在rails应用中的ruby中创建一个有多个关联,其中用户有很多目标并且目标有很多步骤
我似乎无法弄清楚如何为某个目标创建一个步骤。我已经玩了一段时间并在这里四处看看但没有找到解决方案。
以下是我的Goal_controller,Step_Controller,步骤表单和目标表单
目标控制器:
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /goals
# GET /goals.json
def index
@goals = Goal.all
end
# GET /goals/1
# GET /goals/1.json
def show
@goal = Goal.find(params[:id])
session[:current_goal] = @goal.id
end
# GET /goals/new
def new
@goal = Goal.new
end
# GET /goals/1/edit
def edit
end
# POST /goals
# POST /goals.json
def create
@goal = current_user.goals.new(goal_params)
respond_to do |format|
if @goal.save
format.html { redirect_to @goal, notice: 'Goal was successfully created.' }
format.json { render :show, status: :created, location: @goal }
else
format.html { render :new }
format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /goals/1
# PATCH/PUT /goals/1.json
def update
respond_to do |format|
if @goal.update(goal_params)
format.html { redirect_to @goal, notice: 'Goal was successfully updated.' }
format.json { render :show, status: :ok, location: @goal }
else
format.html { render :edit }
format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /goals/1
# DELETE /goals/1.json
def destroy
@goal.destroy
respond_to do |format|
format.html { redirect_to goals_url, notice: 'Goal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_goal
@goal = Goal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def goal_params
params.require(:goal).permit(:Goal, :Description, :Date, :DueDate, :user_id)
end
end
步骤控制器:
class StepsController < ApplicationController
#before_action :set_step, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /steps
# GET /steps.json
def index
@steps = Goal.find(params[:goal_id]).steps.all
end
def new
@step = Goal.find(params[:goal_id]).steps.new
end
# GET /steps/1
# GET /steps/1.json
def show
end
# GET /steps/1/edit
def edit
end
def create
@step = Goal.find(params[:goal_id]).steps.new(step_params)
respond_to do |format|
if @step.save
format.html { redirect_to @step, notice: 'Step was successfully created.' }
format.json { render :show, status: :created, location: @step }
else
format.html { render :new }
format.json { render json: @step.errors, status: :unprocessable_entity }
end
end
redirect_to(goal_steps_url(@goal))
end
def update
@step.update(step_params)
respond_to do |format|
if @step.update(step_params)
format.html { redirect_to @step, notice: 'Step was successfully updated.' }
format.json { render :show, status: :ok, location: @step }
else
format.html { render :edit }
format.json { render json: @step.errors, status: :unprocessable_entity }
end
end
end
# POST /steps
# POST /steps.json
def destroy
@step.destroy
respond_to do |format|
format.html { redirect_to steps_url, notice: 'Step was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_step
@step = Goal.find(params[:goal_id]).Step.find(params[:id])
end
def step_params
params.require(:step).permit(:requirement, :completionTime, :goal_id)
end
end
步骤表格:
<%= form_for(@step) do |f| %>
<% if @step.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@step.errors.count, "error") %> prohibited this step from being saved:</h2>
<ul>
<% @step.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :requirement %><br>
<%= f.text_field :requirement %>
</div>
<div class="field">
<%= f.label :completionTime %><br>
<%= f.number_field :completionTime %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
目标表格:
<%= form_for(@goal) do |f| %>
<% if @goal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@goal.errors.count, "error") %> prohibited this goal from being saved:</h2>
<ul>
<% @goal.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Goal %><br>
<%= f.text_field :Goal %>
</div>
<div class="field">
<%= f.label :Description %><br>
<%= f.text_area :Description %>
</div>
<div class="field">
<%= f.label :Date %><br>
<%= f.date_select :Date %>
</div>
<div class="field">
<%= f.label :DueDate %><br>
<%= f.date_select :DueDate %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:0)
当您提交步骤创建表单时,您似乎会遗漏goal_id
。您需要将其存储在步骤form
中的隐藏字段中,或作为路线的一部分存储(例如POST /goals/10/steps
)。