模特似乎没有被保存?

时间:2016-03-16 04:04:48

标签: ruby-on-rails devise

所以我使用devise gem为我的网络应用生成用户。我和我的用户有一个class Business < ActiveRecord::Base belongs_to :user end 的商业模式。当我在登录我的用户时尝试创建业务时,它根本就不保存它。此外,当我在终端中使用rails控制台试图打开我的商业模式时返回nil。在视图中调用.business上的用户没有显示任何内容

这是我的商业模式

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>

查看业务

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

1 个答案:

答案 0 :(得分:0)

您的商家信息表中是否有<cfdirectory name="getFiles" directory="#tempUploadVideoDIR#" action="list"> <cfdump var="#getFiles#"> 列?它看起来像这样:user_id

然后,要直接将业务与用户关联,请在t.integer "user_id"中以这种方式编写新方法和创建方法:

businesses_controller.rb

希望这有帮助。