工作有很多申请人..但是,我无法将工作参数转到申请人表格

时间:2014-11-30 23:02:12

标签: ruby-on-rails ruby ruby-on-rails-4 relationship form-for

用户可以查看作业,然后填写申请表。 I.E.工作有很多应用。创建新应用程序时,相应的作业属性不会转到表单。

我确实正确设置了关系。我没有正确传递参数。当我创建一个新的应用程序时,它确实找到了job_id。但是,在申请表上,我希望显示更多的工作属性,例如job.title。我认为这是我控制器的一个问题。

applications_controller.rb:

class ApplicationsController < ApplicationController
  before_action :set_application, only: [:show, :edit, :update, :destroy]

  # GET /applications
  # GET /applications.json
  def index
    @applications = Application.all
  end

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

  # GET /applications/new
  def new
    @application = Application.new
    @job = Job.find(params[:job_id])
  end

  # GET /applications/1/edit
  def edit
  end

  # POST /applications
  # POST /applications.json
  def create
    @job = Job.find(params[:job_id])
    @application = Application.new(application_params)
    @application.job = @job
    name = @job.title

    respond_to do |format|
      if @application.save
        format.html { redirect_to @application, notice: 'Application was successfully created.' }
        format.json { render :show, status: :created, location: @application }
      else
        format.html { render :new }
        format.json { render json: @application.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /applications/1
  # DELETE /applications/1.json
  def destroy
    @application.destroy
    respond_to do |format|
      format.html { redirect_to applications_url, notice: 'Application was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def application_params
      params[:application]
    end

     def job_params
      params.require(:job).permit(:title, :description, :status)
    end
end

这是我在jobs / index.html.erb中的表格 - &gt;它有启动新应用程序的链接......我应该在这里传递局部变量吗?

  <table class="table table-striped">
    <thead>
      <tr>
        <th colspan="3"> Current Job Openings</th>
      </tr>
    </thead>

    <tbody>
      <% @jobs.each do |job| %>
        <tr>
          <td><%= job.title %></td>
          <td><%= job.description  %></td>
          <td><button type="button" class="newave-button medium grey move pull-right" style="color: white"><%= link_to 'Online Application', new_job_application_path(job) %></button></td>
        </tr>
      <% end %>
    </tbody>
  </table>

这实际上启动了applications / new.html.erb,它只包含一个部分来呈现一个新表单:

<%= render 'form' %>

最后,这是我正在呈现的应用程序/ _form.html.erb。我想将页面顶部的工作特定信息联系起来..

<%= link_to 'Back', jobs_path, class: "newave-button medium grey move" %>

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

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

  <div class="container small-width" id="application-form">

    <h2>Personal Information</h2>
  <%= params %>
  <div class="field">
    <%= f.label :full_name, "Name" %><br>
    <%= f.text_field :full_name%>
  </div>
  <div class="actions">
    <%= f.submit "Submit Form", class: 'btn btn-primary' %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

总而言之,您在创建Application对象时尝试将作业标题添加到Application表单/参数映射中,以便应用程序中也包含作业标题。

根据我们的讨论和您的描述,job_title字段是作业模型的一个属性,并且与应用程序本身无关。 Application和Job之间的关联是通过Application表上的一个列调用的(除非您故意更改了名称)job_id。如果检查呈现的表单的操作URL,它已经包含job_id,当您在参数映射中提交表单时,它可用于rails。正如您在控制器中的create操作中所做的那样,您可以通过params[:job_id]访问它。基本上,你有正确的想法create行动。

以这种方式思考; Application形式中必需的唯一参数(因此,在参数中)是与Application对象直接相关的参数。作业ID 相关,因为您需要知道您正在关联哪个作业对象,但作业标题不是,因为您可以通过相关作业获取它。

关于如何在new表单中访问作业标题以进行显示的问题,如上所述,您已经在控制器中设置了@job变量。您可以像这样简单地访问职位:@job.title@application.job.title不起作用的原因是因为我们尚未关联它。请注意,newcreate操作是完全独立的,并且在create操作中,您将进行该关联。