嵌套资源表单,拉取索引字段

时间:2014-04-24 10:07:54

标签: ruby-on-rails ruby ruby-on-rails-4

我有两个模型,其中一个是嵌套的。 '乔布斯'在'坦克'里面。我正在表格中在'Tank'中创建一个新的'Job',而不是将tank_id拉入我的_form。在过去,我将current_user作为表单的参数调用,并且工作正常,但这完全不同,因为这个小项目没有用户,可以这么说。只是一个简单的跟踪系统。在过去,表单已经填写了model_id的表单字段,因为它已传递给表单。我不确定自己做错了什么,但在谈到这一点时我还很简陋,所以请保持温和......

tank.tb

class Tank < ActiveRecord::Base
  has_many :jobs
end

job.rb

class Job < ActiveRecord::Base
  belongs_to :tank
end

jobs_controller.rb

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]

  # GET /jobs
  # GET /jobs.json
  def index
    @jobs = Job.all
  end

  # GET /jobs/1
  # GET /jobs/1.json
  def show
  end

  # GET /jobs/new
  def new
    @tank = Tank.find(params[:tank_id])
    @job = Job.new
  end

  # GET /jobs/1/edit
  def edit
  end

  # POST /jobs
  # POST /jobs.json
  def create
    @job = Job.new(jobs_params)

    respond_to do |format|
      if @job.save
        format.html { redirect_to tank_jobs_path(:tank_id, @job), notice: 'Job was successfully created.' }
        format.json { render :show, status: :created, location: @job }
      else
        format.html { render :new }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /jobs/1
  # PATCH/PUT /jobs/1.json
  def update
    respond_to do |format|
      if @job.update(job_params)
        format.html { redirect_to @job, notice: 'Job was successfully updated.' }
        format.json { render :show, status: :ok, location: @job }
      else
        format.html { render :edit }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /jobs/1
  # DELETE /jobs/1.json
  def destroy
    @job.destroy
    respond_to do |format|
      format.html { redirect_to tank_jobs_path(:tank_id) }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_job
      @job = Job.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def job_params
      params.require(:job).permit(:tank_id, :jobnum)
    end
end

应用程序/视图/作业/ _form.html.erb

<%= form_for ([@tank, @job]) do |f| %>
  <% if @job.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>

      <ul>
      <% @job.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :tank_id %><br>
    <%= f.number_field :tank_id %>
  </div>
  <div class="field">
    <%= f.label :jobnum %><br>
    <%= f.text_field :jobnum %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

编辑 - 包括我的tank_show视图:

<p id="notice"><%= notice %></p>

<p>
  <strong>Gastype:</strong>
  <%= @tank.gastype %>
</p>

<p>
  <strong>Level:</strong>
  <%= @tank.level %>
</p>

<%= link_to 'Edit', edit_tank_path(@tank) %> |
<%= link_to 'Back', tanks_path %> |
<%= link_to 'New Job Assignment', new_tank_job_path(@tank,@job) %>

0 个答案:

没有答案