如何将url中的params传递给rails中的另一个资源?

时间:2015-05-25 09:21:04

标签: ruby-on-rails parameters

我正在开发一种工作板,我有一个名为'jobdetails'的“页面”,其中包含作业详细信息('jobdetails'显示开放资源)作业,应用按钮应该重定向到'jobapplications'“page”,其中求职者可以上传他的简历(尚未实施)并保存opening_id。

我尝试了很多方法,但仍然没有工作。

我说我想用url传递params,但我打开其他方式,模型或控制器。

感谢。

编辑:我可以保存记录,但opening_id仍为空白......

Rails.application.routes.draw do
  resources :posts
    devise_for :jobseekers, controllers: { registrations: "jobseekers/registrations", sessions: "jobseekers/sessions" }
  resources :companypics
  devise_for :users, controllers: { registrations: "registrations" }
  resources :openings 
  resources :jobapplications
  resources :companies
  resources :locations
  resources :timesheets
  resources :expenses
  resources :jobdescriptions
  resources :jobdetails 
  root :to => 'dashbords#index'
  end

class JobapplicationsController < ApplicationController
  before_filter :authenticate_jobseeker!

  layout 'apply'
def applyparam
  @opening = Opening.find(params[:opening])

end

  def index
    @jobapplications = Jobapplication.all
  end

  def show
  end

  def new
    @jobapplication = Jobapplication.new
  end

  def edit
  end

  def create
    @jobapplication = current_jobseeker.jobapplications.create(jobapplication_params)
    @jobapplication.save
    redirect_to new_jobapplication_path
  end

  def update
    @jobapplication.update(jobapplication_params)
  end

  def destroy
    @jobapplication.destroy
  end

  private
    

    def jobapplication_params
      params.require(:jobapplication).permit(:opening_id, :jobseeker_id, )
    end
end

class JobdetailsController < ApplicationController
  layout 'apply'

  def show
    @jobdetail = Opening.find_by_applyjobid(params[:applyjobid])
    @logo = current_user.company.logo.url

  end
  end

Jobdetails Show

<div class='login-container'>
  <div class='container'>
    <div class='row'>
      <div class='col-sm-4 col-sm-offset-4'>
        <div class='container'>
        <div class='.col-xs-6 .col-md-4'>
          <div class='row'>
                   <h1>
              <strong><%= @jobdetail.jobdescription.job_title %></strong>
            </h1>
            <br/>
            <b>Company:   </b>
            <br/>
            <%= @jobdetail.company.company_name %>
            <br> </br>
            <%= image_tag @logo  %>     
            <br>
            <b>Job Location:   </b>
            <br/>
            <%= @jobdetail.location.name %> <%= @jobdetail.location.postalcode %>
            <br>
            <br/>
            <b>Job salary:   </b>
            <br/>
            <%= @jobdetail.jobdescription.rate_pay %>
            <br>
            <br/>
              <p class="jobsummary"><b>Job Summary:   </b>
                <br/>
                <%= @jobdetail.jobdescription.job_summary %></p>
            <br>
            <br/>
            <% if !@jobdetail.jobdescription.shift.blank? -%>
          <b>Job shift:   </b>
                <br/>
            <%= @jobdetail.jobdescription.shift %>
            <br>
            <br/>
            <% end %>
            <br>

            <b>Job requirements:   </b>
            <br/>

            <% if !@jobdetail.jobdescription.job_duties.blank? -%>
               <%= @jobdetail.jobdescription.job_duties %>
            <br>
            <br/>
            <% end %>

            <% if !@jobdetail.jobdescription.tasks.blank? -%>
            <%= @jobdetail.jobdescription.tasks %>
            <br>
            <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.job_duties.blank? -%>
                <%= @jobdetail.jobdescription.job_duties %>
            <br>
            <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.responsibilities.blank? -%>
                <%= @jobdetail.jobdescription.responsibilities %>
            <br>
            <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.results.blank? -%>

            <%= @jobdetail.jobdescription.results %>
            <br>
            <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.skills.blank? -%>

          <%= @jobdetail.jobdescription.skills %>
            <br>
            <br/>
            <% end %>

            <% if !@jobdetail.jobdescription.knowledge.blank? -%>

          <%= @jobdetail.jobdescription.knowledge %>
            <br>
            <br/>
            <% end %>

            <% if !@jobdetail.jobdescription.abilities.blank? -%>
          <%= @jobdetail.jobdescription.abilities %>
            <br>
            <br/>
            <% end %>
            <br>

            <b>Education:   </b>
            <br/>
            <% if !@jobdetail.jobdescription.education_and_training.blank? -%>
                <%= @jobdetail.jobdescription.education_and_training %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.qualifications.blank? -%>
                <%= @jobdetail.jobdescription.qualifications %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.license_certification.blank? -%>
                <%= @jobdetail.jobdescription.license_certification %>
                <br>
                <br/>
            <% end %>
            <br>

            <b>Job environment:   </b>
            <br/>
            <% if !@jobdetail.jobdescription.physical_requirement.blank? -%>
                <%= @jobdetail.jobdescription.physical_requirement %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.work_envir_condition.blank? -%>
                <%= @jobdetail.jobdescription.work_envir_condition %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.protective_clothing_and_devices_required.blank? -%>
                <%= @jobdetail.jobdescription.protective_clothing_and_devices_required %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.tools_or_equipment_required.blank? -%>
                <%= @jobdetail.jobdescription.tools_or_equipment_required %>
                <br>
                <br/>
            <% end %>
            <br>

            <b>Benefits:   </b>
            <br/>
            <% if !@jobdetail.jobdescription.benefits.blank? -%>
                <%= @jobdetail.jobdescription.benefits %>
                <br>
                <br/>
            <% end %>
            <% if !@jobdetail.jobdescription.other_benefits.blank? -%>
                <%= @jobdetail.jobdescription.other_benefits %>
                <br>
                <br/>
            <% end %>

            <br>
            <br/>

            <%= link_to "Apply now", new_jobapplication_path(:opening => @jobdetail.id) %>

<%= form_for (@jobapplication) do |f| %>
    <div class='login-container'>
  <div class='container'>
    <div class='row'>
      <div class='col-sm-4 col-sm-offset-4'>
        <h1 class='text-center title'>Apply</h1>
        <%= render 'layouts/messages' %>
        <div class='form-group'>

          </div>
        </div>
<div class="form-group">
<%= f.hidden_field :opening %>
        <%= f.submit "Submit", class: "btn btn-large btn-success" %>
        <% end %>
        </form>

class Jobapplication < ActiveRecord::Base
  belongs_to :opening

end

create_table "jobapplications", force: true do |t|
    t.integer  "opening_id"
    t.integer  "jobseeker_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Started POST "/jobapplications" for 127.0.0.1 at 2015-05-25 10:49:38 +0100
Processing by JobapplicationsController#create as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"+0z1IIG0COeYZUQavPUZXu/5m316t9VrUs9eiOFrMjA=", "jobapplication"=>{"opening"=>"#<Opening:0x85cc
f80>"}, "commit"=>"Submit your timesheet"}
  Jobseeker Load (1.0ms)  SELECT  "jobseekers".* FROM "jobseekers"  WHERE "jobseekers"."id" = 1  ORDER BY "jobseekers"."id" ASC LIMIT 1
Unpermitted parameters: opening
   (0.0ms)  begin transaction
  SQL (2.0ms)  INSERT INTO "jobapplications" ("created_at", "jobseeker_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-05-25 09:49:39.
818752"], ["jobseeker_id", 1], ["updated_at", "2015-05-25 09:49:39.818752"]]
   (131.0ms)  commit transaction
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
Redirected to http://localhost:3000/jobapplications/new
Completed 302 Found in 591ms (ActiveRecord: 134.0ms)

1 个答案:

答案 0 :(得分:0)

在您的观点中,将opening更改为opening_id

<%= link_to "Apply now", new_jobapplication_path(opening_id: @jobdetail.id) %>

new动作实例化opening_id

def new
  @jobapplication = Jobapplication.new(opening_id: params[:opening_id])
end