0

它可能是重複的,例如, this但我的代碼不工作,沒有任何錯誤,這將指導我。 我得到了廣告,其中有一些參考其他模型(公司[:id,:name])和其他類似的。此型號有has_many :advert聲明。 我想製作一個表單,用戶將在這個表單中創建廣告。市也有belongs_to :province如何創建所有需要的記錄,以主要填寫字段填寫網頁表單中的字段

class Advert < ActiveRecord::Base 
    belongs_to :company 
    belongs_to :job_category 
    belongs_to :city 
    belongs_to :site 

    accepts_nested_attributes_for :city 
    accepts_nested_attributes_for :company 
    accepts_nested_attributes_for :job_category 

    validates :company, presence: true 
    validates :city, presence: true 
    validates :url, presence: true, uniqueness: true 
    validates :date_link_added, presence: true 
    validates :appointment, presence: true 
    validates :site, presence: true 
    validates :job_category, presence: true 
    validates :date_last_verified, presence: true 
end 

我有視圖/ new.html.erb adverts_controller我在其中具有這樣的代碼:

<%= bootstrap_form_for(@advert, label_col: "col-sm-6", control_col: "col-sm-6") do |fb| %> 
        <%= fb.url_field :url, label: "Original Link do advert" %> 
        <%= fb.fields_for :job_category do |j| %> 
        <%= j.text_field :name, label: "Job Category" %> 
        <% end %> 
        <%= fb.fields_for :city do |c| %> 
        <%= c.collection_select :province_id, Province.all, :id, :name, {prompt: "Select ...", label: "Province"}, { class: "selectpicker form-control" } %> 
        <%= c.text_field :name, label: "City" %> 
        <% end %> 
        <%= fb.fields_for :company do |c| %> 
        <%= c.text_field :name, label: "Company" %> 
        <% end %> 
        <%= fb.text_field :appointment, label: "Appointment" %> 
        <%= fb.date_field :date_adv_added, label: "Release" %> 
        <%= fb.date_field :date_expiration, label: "Expire" %> 
        <%= fb.form_group :verified do %> 
         <%= fb.check_box :verified, label: "Verified" %> 
        <% end %> 
        <%= link_to "<- Cancel", adverts_path, class: "btn btn-default" %> 
        <div class="pull-right"> 
        <%= fb.submit "Create", class: "btn btn-primary" %> 
        </div> 
       <% end %> 

代碼在控制器

def create 
    ad = advert_params 
    @advert = Advert.new(ad) 
    if(@advert.save) 
     redirect_to root_path 
    else 
     redirect_to :back, alert: "Query failed. Check if all fields filled and try again later" 
    end 
    end 

    private 
    def advert_params 
    adv_params = params.require(:advert).permit(:url, :date_adv_added, :date_expiration, :appointment, :verified, company_attributes: [:id, :name], job_category_attributes: [:id, :name], city_attributes: [:id, :name, :province_id]) 
    end 

@ advert.save是所有的時間返回錯誤。 在控制檯上我只得到:

Advert Load (0.2ms) SELECT `adverts`.* FROM `adverts` 
    JobCategory Load (0.1ms) SELECT `job_categories`.* FROM `job_categories` WHERE `job_categories`.`id` = 3 LIMIT 1 
    City Load (0.2ms) SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 3 LIMIT 1 
    Province Load (0.2ms) SELECT `provinces`.* FROM `provinces` WHERE `provinces`.`id` = 2 LIMIT 1 
    Company Load (0.1ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 3 LIMIT 1 
    JobCategory Load (0.2ms) SELECT `job_categories`.* FROM `job_categories` WHERE `job_categories`.`id` = 4 LIMIT 1 
    City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 4 LIMIT 1 
    Province Load (0.2ms) SELECT `provinces`.* FROM `provinces` WHERE `provinces`.`id` = 13 LIMIT 1 
    Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 4 LIMIT 1 
    JobCategory Load (0.2ms) SELECT `job_categories`.* FROM `job_categories` WHERE `job_categories`.`id` = 5 LIMIT 1 
    City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 5 LIMIT 1 
    Province Load (0.2ms) SELECT `provinces`.* FROM `provinces` WHERE `provinces`.`id` = 6 LIMIT 1 
    Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 5 LIMIT 1 
    JobCategory Load (0.2ms) SELECT `job_categories`.* FROM `job_categories` WHERE `job_categories`.`id` = 6 LIMIT 1 
    City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE `cities`.`id` = 6 LIMIT 1 
    Province Load (0.2ms) SELECT `provinces`.* FROM `provinces` WHERE `provinces`.`id` = 15 LIMIT 1 
    Company Load (0.2ms) SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 6 LIMIT 1 
    Rendered adverts/index.html.erb within layouts/application (31.6ms) 
Completed 200 OK in 143ms (Views: 138.5ms | ActiveRecord: 3.1ms) 

並沒有什麼被添加到數據庫中。 我不知道我錯過了什麼。 我沒有最後更改的代碼是JAG 任何人都有線索可能是錯誤的?

回答

0

嘗試在create操作中添加打印語句,如下所示並檢查控制檯。這應該列出任何驗證錯誤,這應該給你一個關於什麼導致失敗保存的想法。

def create 
    ad = advert_params 
    @advert = Advert.new(ad) 

    if(@advert.save) 
    redirect_to root_path 
    else 
    $stderr.puts @advert.errors.messages 
    redirect_to :back, alert: "Query failed. Check if all fields filled and try again later" 
    end 
end 
+0

{:date_link_added => [ 「不能爲空」],:現場=> [ 「不能爲空」],:job_category => [ 「不能爲空」]} – Levvy

+0

現在你知道哪些字段丟失了。如果你解決這些錯誤,你應該很好。 – Dharam

+0

好的。 date_link_added和網站我需要手動添加,但job_category應該像城市和公司一樣傳遞,但事實並非如此。 – Levvy

相關問題