2015-05-25 45 views
1

我正在開發一種工作板,在那裏我有一個名爲'jobdetails'的工作細節('jobdetails'顯示開口資源)的工作與應用按鈕,應該重定向到'jobapplications'「頁面」,求職者可以上傳他的cv(尚未實現)並保存opens_id。如何將URL中的參數傳遞給rails中的另一個資源?

我嘗試了很多方法,但仍然無法正常工作。

我說我想通過參數傳遞的網址,但我用其他方式,模型或控制器打開。

謝謝。

編輯:我能保存記錄,但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 [email protected]? -%> 
 
      <b>Job shift: </b> 
 
       <br/> 
 
      <%= @jobdetail.jobdescription.shift %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <br> 
 

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

 
      <% if [email protected]_duties.blank? -%> 
 
       <%= @jobdetail.jobdescription.job_duties %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 

 
      <% if [email protected]? -%> 
 
      <%= @jobdetail.jobdescription.tasks %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <% if [email protected]_duties.blank? -%> 
 
       <%= @jobdetail.jobdescription.job_duties %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <% if [email protected]? -%> 
 
       <%= @jobdetail.jobdescription.responsibilities %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <% if [email protected]? -%> 
 

 
      <%= @jobdetail.jobdescription.results %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <% if [email protected]? -%> 
 

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

 
      <% if [email protected]? -%> 
 

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

 
      <% if [email protected]? -%> 
 
      <%= @jobdetail.jobdescription.abilities %> 
 
      <br> 
 
      <br/> 
 
      <% end %> 
 
      <br> 
 

 
      <b>Education: </b> 
 
      <br/> 
 
      <% if [email protected]_and_training.blank? -%> 
 
       <%= @jobdetail.jobdescription.education_and_training %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]? -%> 
 
       <%= @jobdetail.jobdescription.qualifications %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]_certification.blank? -%> 
 
       <%= @jobdetail.jobdescription.license_certification %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <br> 
 

 
      <b>Job environment: </b> 
 
      <br/> 
 
      <% if [email protected]_requirement.blank? -%> 
 
       <%= @jobdetail.jobdescription.physical_requirement %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]_envir_condition.blank? -%> 
 
       <%= @jobdetail.jobdescription.work_envir_condition %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]_clothing_and_devices_required.blank? -%> 
 
       <%= @jobdetail.jobdescription.protective_clothing_and_devices_required %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]_or_equipment_required.blank? -%> 
 
       <%= @jobdetail.jobdescription.tools_or_equipment_required %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <br> 
 

 
      <b>Benefits: </b> 
 
      <br/> 
 
      <% if [email protected]? -%> 
 
       <%= @jobdetail.jobdescription.benefits %> 
 
       <br> 
 
       <br/> 
 
      <% end %> 
 
      <% if [email protected]_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)

+0

您想在哪個操作中接收參數?並從哪裏? –

+0

嗨,從Jobdetails#顯示到工作申請#新謝謝 – reme

回答

0

在你看來變化openingopening_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 
+0

再次感謝,但仍然參數丟失或價值爲空:jobapplication – reme

+0

我整理出來了! 「def new @jobapplication = Jobapplication.new(:opening => params [:opening]) end」I aso added「<%= hidden_​​field(:jobapplication,:opening,:value => params [:opening])% >「謝謝你的幫助 – reme

+0

太好了......永遠不要放棄! :) –

相關問題