Ruby on Rails Goal 有-many Steps 关联。如何为目标创建步骤?

Posted

技术标签:

【中文标题】Ruby on Rails Goal 有-many Steps 关联。如何为目标创建步骤?【英文标题】:Ruby on Rails Goal has-many Steps association. How to create steps for a goal? 【发布时间】:2016-05-05 02:24:23 【问题描述】:

我正在尝试在 ruby​​ on rails 应用程序中创建 has-many 关联,其中用户有很多目标,而目标有很多步骤

我似乎无法弄清楚如何为特定目标创建步骤。我一直在玩它一段时间并在这里四处寻找,但还没有找到解决方案。

下面是我的 Goal_controller、Step_Controller、Step 表单和 Goal 表单

目标控制器:

    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】:

提交步骤创建表单时,您似乎会丢失goal_id。您需要将其存储在步骤 form 的隐藏字段中,或作为路径的一部分(例如 POST /goals/10/steps)。

【讨论】:

我尝试添加它,但现在我收到错误“未定义的方法 `steps_path' for #:0x007fcdbd770ac8>”行“"。

以上是关于Ruby on Rails Goal 有-many Steps 关联。如何为目标创建步骤?的主要内容,如果未能解决你的问题,请参考以下文章

ruby on rails has_many 关系表单验证孩子

ruby-on-rails - 嵌套资源问题

如何在 Ruby on Rails 中通过关联订购 has_many?

如何在 Ruby on Rails 6 中正确应用 has_many 关系和 order by

Ruby on Rails,一对多关联(One-to-Many)

微博在用户页面上的评论(Ruby on Rails)