Ruby on Rails цель имеет-много шагов ассоциации. Как создать шаги для цели?

Я пытаюсь создать ассоциацию has-many в приложении ruby on rails, где у пользователя-много целей, а у цели-много шагов

Я, кажется, не могу понять, как связать создание шага для определенной цели. Я играл с ним некоторое время и смотрел вокруг, но не нашел решения.

Ниже приведены мои 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 %>

1 ответ

  1. Похоже, вам будет не хватать вашегоgoal_id, когда вы отправляете свою форму создания шага. Вам нужно будет сохранить его либо в скрытом поле на вашем шагеform, либо как часть маршрута (например POST /goals/10/steps).