查询表格中的图像/文本到视图

时间:2014-07-23 05:56:00

标签: ruby-on-rails ruby devise rails-activerecord carrierwave

我正在尝试为用户创建照片上传视图。我是铁杆的新手,所以我不确定我是否正确地做这件事,因为我不太确定所有的部件是否合适。我正在使用devisecarrierwave进行数据库中的用户身份验证和图像存储。我不太确定如何处理params IncomePicture_params。我想创建一个视图,允许我调用和显示user

图片的图像和文本

我正在使用rails 4

型号:

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

  validates_presence_of :username

  has_many :expense_pictures 
  has_many :income_pictures

end

class IncomePicture < ActiveRecord::Base
  belongs_to :user
  mount_uploader :image, ImageUploader
  has_one :income_text
  accepts_nested_attributes_for :income_text
end

class IncomeText < ActiveRecord::Base
  belongs_to :income_picture
end

控制器:

class UserController < ApplicationController

  def create
    User.create(user_params)
  end


  private

    def user_params
      # required input for params
      # permit - returns a version of the params hash with ony the permitted attributes
      params.require(:user).permit(:name, :email, :password, :password_confirmation, )
    end

end

class IncomePicturesController < ApplicationController

  def new 
    @income_picture = IncomePicture.new(IncomePicture_params)
  end

  def create
  end

  def destroy
  end

  private

    def IncomePicture_params
      params.require(:income_picture).permit(:image, income_text_attributes: [:amount])
    end
end

模式

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

  create_table "income_pictures", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image"
    t.integer  "user_id"
  end

  add_index "income_pictures", ["user_id"], name: "index_income_pictures_on_user_id"

  create_table "income_texts", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "income_picture_id"
    t.string   "amount"
  end

  add_index "income_texts", ["income_picture_id"], name: "index_income_texts_on_income_picture_id"

  create_table "users", force: true do |t|
    t.string   "email",               default: "", null: false
    t.string   "encrypted_password",  default: "", null: false
    t.datetime "remember_created_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "username"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

end

0 个答案:

没有答案