2015-04-02 33 views
0

我是新來的rails,所以任何幫助將不勝感激。我有模特用戶(招聘人員)和廣告。用戶has_many廣告和廣告屬於用戶。無法將user_id鏈接到新創建的對象 - rails 4

Question: when a userr creates an advert i want the created advert to be automatically linked to the userr that created the advert. I am unsure how to go about this in the controller

我知道如何在控制檯中爲用戶分配廣告。但不確定如何做到這一點的控制器

控制檯

advert = Advert.first 
advert.userr_id = 3 
advert.save 

路線

Rails.application.routes.draw do 
    resources :adverts 

    resources :feedbacks 

    devise_for :userrs 
    devise_for :userjs 
    root 'static_pages#homepg' 
    get  'affiliate', to: 'static_pages#affiliatepg' 
    get  'terms',  to: 'static_pages#termpg' 
    get  'privacy',  to: 'static_pages#privacypg' 
    get  'contact',  to: 'static_pages#contactpg' 
    get  'about',  to: 'static_pages#aboutpg' 
    get  'recruiters', to: 'static_pages#recruiterpg' 
    get  'jobseekers', to: 'static_pages#jobseekerpg' 
    get  'approach',  to: 'static_pages#approachpg' 
    get  'sector',  to: 'static_pages#sectorpg' 
    get  'news',   to: 'static_pages#newspg' 
    get  'offer',  to: 'static_pages#offerpg' 
    get  'refferal',  to: 'static_pages#refferalpg' 
end 

我把下面的代碼: 在我的廣告控制器的創造作用下:

def create 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.build(params[:advert]) 
    if @advert.save 
     flash[:notice] = 'Successfully created advert' 
     redirect_to recruiters_path 
    else 
     render action: 'new' 
    end 
    end 

但我得到了錯誤:

Couldn't find Userr without an ID

日誌 - 錯誤消息

Started GET "/adverts/new" for 127.0.0.1 at 2015-04-02 14:59:38 +0100 
Processing by AdvertsController#new as HTML 
Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms) 

ActiveRecord::RecordNotFound (Couldn't find Userr without an ID): 
    app/controllers/adverts_controller.rb:20:in `new' 


    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (8.2ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (3.4ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (0.8ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (0.8ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (27.3ms) 


Started GET "/adverts/new" for 127.0.0.1 at 2015-04-02 14:59:38 +0100 
Processing by AdvertsController#new as HTML 
Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms) 

ActiveRecord::RecordNotFound (Couldn't find Userr without an ID): 
    app/controllers/adverts_controller.rb:20:in `new' 


    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (8.1ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (3.0ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (0.9ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (0.8ms) 
    Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (26.2ms) 

架構

ActiveRecord::Schema.define(version: 20150330233948) do 

    create_table "adverts", force: true do |t| 
    t.string "title" 
    t.text  "content" 
    t.integer "category_jobtype_id" 
    t.integer "category_positiontype_id" 
    t.integer "salarystart" 
    t.integer "salaryend" 
    t.integer "category_country_id" 
    t.string "city" 
    t.string "town" 
    t.string "postcode" 
    t.integer "category_editorialapproval_id" 
    t.integer "category_applicationrequest_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "userr_id" 
    end 

    create_table "userrs", force: true do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "firstname" 
    t.string "lastname" 
    t.string "companyname" 
    t.integer "category_businesstype_id" 
    end 

    add_index "userrs", ["email"], name: "index_userrs_on_email", unique: true 
    add_index "userrs", ["reset_password_token"], name: "index_userrs_on_reset_password_token", unique: true 

模型

class Advert < ActiveRecord::Base 
    belongs_to :user 
end 

class Userr < ActiveRecord::Base 
    has_many :adverts 
end 

控制器:廣告

class AdvertsController < ApplicationController 
    respond_to :html, :xml, :json 
    before_action :set_advert, only: [:show, :edit, :update, :destroy] 

    def index 
    @adverts = Advert.all 
    respond_with(@adverts) 
    end 

    def show 
    respond_with(@advert) 
    end 

    # def new 
    # @advert = Advert.new 
    # respond_with(@advert) 
    # end 

    def new 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.build 
    respond_with(@advert) 
    end 

    def edit 
    end 

    # def create 
    # @advert = Advert.new(advert_params) 
    # @advert.save 
    # respond_with(@advert) 
    # end 

    def create 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.build(params[:advert]) 
    if @advert.save 
     flash[:notice] = 'Successfully created advert' 
     redirect_to recruiters_path 
    else 
     render action: 'new' 
    end 
    end 

    def update 
    @advert.update(advert_params) 
    respond_with(@advert) 
    end 

    def destroy 
    @advert.destroy 
    respond_with(@advert) 
    end 

    private 
    def set_advert 
     @advert = Advert.find(params[:id]) 
    end 

    def advert_params 
     params.require(:advert).permit(:title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id) 
    end 
end 

形式的廣告

<%= simple_form_for(@advert) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :title %> 
    <%= f.input :content %> 
    <%= f.association :category_jobtype, collection: CategoryJobtype.all, prompt: "select a category" %> 
    <%= f.association :category_positiontype, collection: CategoryPositiontype.all, prompt: "select a category" %> 
    <%= f.input :salarystart %> 
    <%= f.input :salaryend %> 
    <%= f.association :category_country, collection: CategoryCountry.all, prompt: "select a category" %> 
    <%= f.input :city %> 
    <%= f.input :town %> 
    <%= f.input :postcode %> 
    <%= f.association :category_editorialapproval, as: :radio_buttons %> 
    <%= f.association :category_applicationrequest, as: :radio_buttons %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

任何幫助將不勝感激

+0

您能否展示您用於創建廣告的表單? – 2015-04-02 13:34:39

+4

Yourr用兩個r的用戶拼寫正在加劇我的困惑。 – 2015-04-02 13:35:16

+0

張貼路線文件 – argentum47 2015-04-02 13:39:26

回答

1

首先,你需要添加userr_id到PARAMS:

def advert_params 
    params.require(:advert).permit(:userr_id, :title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id) 
end 

二,添加userr_id到窗體:

<%= simple_form_for(@advert) do |f| %> 
    <%= f.hidden_field(:userr_id) %> 

三,create改變帕拉姆名稱:

def create 
    @userr = Userr.find(params[:advert][:userr_id]) 

您可以通過routes做同樣的,在不改變形式。它更清潔,但需要更多源代碼更改。我不能在這裏給你完整的語法,但它看起來就像下面:

#routes.rb 
resources :userrs do 
    resources :addverts 
end 

而且廣告的形式將成爲/ userrs/[用戶ID] /廣告,所以你就可以從url中獲取用戶標識而不是表單域。但是您需要更改所有路徑快捷方式。

+0

謝謝@oleg,非常有幫助,不知道我不得不作出許多調整 - 我假設控制器的廣告變化足夠了....但是,謝謝....我做得到一個錯誤,但你的指導是絕對足夠好和可以理解的,以幫助我通過錯誤來徹底解決它。 – ARTLoe 2015-04-02 14:27:23

+0

@ARTLoe可能我錯過了一些東西,請在評論中留下細節。主要的一點是AdvertsController不知道任何關於:userr_id的內容,除非你通過form和params傳遞它。 – 2015-04-02 14:30:44