首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby on Rails目标有多个步骤关联。如何为目标创建步骤?

Ruby on Rails目标有多个步骤关联。如何为目标创建步骤?
EN

Stack Overflow用户
提问于 2016-05-05 10:24:23
回答 1查看 81关注 0票数 0

我正在尝试在ruby on rails应用程序中创建一个多个关联,其中用户有多个目标,一个目标有多个步骤

我似乎想不出如何将创建一个步骤与某个目标联系起来。我已经尝试了一段时间,并在这里四处寻找,但还没有找到解决方案。

下面是我的Goal_controller、Step_Controller、Step form和Goal form

目标控制器:

代码语言:javascript
复制
    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

步长控制器:

代码语言:javascript
复制
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

步骤形式:

代码语言:javascript
复制
<%= 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 %>

目标表单:

代码语言:javascript
复制
<%= 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 %>
EN

回答 1

Stack Overflow用户

发布于 2016-05-05 11:13:47

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37041286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档