2016-03-16 39 views
0

所以我使用devise gem爲我的web應用程序生成用戶。我與我的用戶有一個has_one: association的商業模式。當我登錄到我的用戶時嘗試創建一個企業時,它不會保存它。另外,當我在終端試圖打開我的商業模式使用rails控制檯返回零。在正在調用.business用戶視圖顯示什麼模型似乎沒有得到保存?

這裏是我的商業模式

class Business < ActiveRecord::Base 
    belongs_to :user 
end 

用戶模型

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    validates :name, presence: true 
    has_many :posts, dependent: :destroy 
    has_one :business, dependent: :destroy 

    has_attached_file :avatar, 
            styles: { medium: "300x300#", thumb: "100x100#", post_pic: "44x44" }, 
            default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 
end 

業務控制器

class BusinessesController < ApplicationController 
    before_action :set_business, only: [:show, :edit, :update, :destroy] 

    def index 
    @businesses = Business.all 
    end 

    def show 
    end 

    def new 
    @business = current_user.build_business 
    end 

    def edit 
    end 

    def create 
    @business = current_user.build_business(business_params) 

    if @business.save 
     redirect_to @business, notice: 'Business was successfully created.' 
    else 
     render :new 
    end 
    end 

    def update 
    if @business.update(business_params) 
     redirect_to @business, notice: 'Business was successfully updated.' 
    else 
     render :edit 
    end 
    end 

    def destroy 
    @business.destroy 
    redirect_to businesses_url, notice: 'Business was successfully destroyed.' 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def business_params 
     params.require(:business).permit(:name, :desc, :avatar) 
    end 
end 

查看調用業務

<div class="row"> 
    <div class="col-md-7"> 
     <div class="panel panel-default"> 
      <div class="panel-body"> 
        <%= image_tag current_user.business.avatar.url(:thumb), class:"img-thumbnail" %> 
        <%= current_user.business.name %> 
        <%= current_user.business.desc %> 
        <%= link_to 'create business', new_business_path, class:"pull-right" %> 
       </div> 
     </div> 
    </div> 
</div> 
+0

保存時出現什麼錯誤?你可以檢查@ business.errors – LHH

+0

多數民衆贊成在沒有錯誤的東西,它只是沒有出現。非常奇怪..讓我補充一下這個我稱之爲的觀點。 –

+0

業務表單視圖在哪裏? – LHH

回答

0

你有你的業務表中的列user_id?它應該是這樣的:t.integer "user_id"

然後,直接將業務與用戶聯繫起來,在businesses_controller.rb我會寫新創建這樣的方法:

def new 
    @business = current_user.businesses.new 
    end 

    def create 
    @business = current_user.businesses.new(business_params) 
    if @business.save 
     redirect_to @business, notice: 'Business was successfully created.' 
    else 
     render :new 
    end 
    end 

希望這有助於。

+0

不要相信,我的類型has_one協會工作。它劑量識別它 –

相關問題