控制器#创建 - 无法找到具有' id' =的用户

时间:2016-02-04 23:16:01

标签: ruby-on-rails ruby-on-rails-4 associations

我正在学习web-dev和rails,并且已经停留了几天。

我有两个模型 - UserProperty,并且我认为有正确的设置关联。

我尝试做的只是在property已登录并且user属于该property时才允许创建user "Couldn't find User..." }。我获得了部分签名,但是当我去创建属性时,它会给我DB SQL Browser错误。

我使用class PropertiesController < ApplicationController before_action :authenticate_user!, only: [:new] def index @properties = Property.all end def new @property = Property.new end def create @user = User.find(params[:user_id]) @property = @user.properties.new(property_params) respond_to do |format| if @property.save format.html { redirect_to @property, notice: 'Property was successfully created.' } format.json { render :show, location: @property} else format.html { render :new } format.json end end end def show @property = Property.find(params[:id]) end def edit end def update end def destroy end private def property_params params.require(:property).permit(:street, :city, :province, :postal_code, :description, :picture) end end 验证是否有用户并且有用户ID,因此无法理解登录后无法找到的原因。

感谢任何帮助。

控制器:

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

  create_table "properties", force: :cascade do |t|
t.string   "street"
t.string   "city"
t.string   "province"
t.string   "postal_code"
t.text     "description"
t.datetime "created_at",  null: false
t.datetime "updated_at",  null: false
t.integer  "property_id"
t.string   "picture"
t.integer  "user_id"
end

add_index "properties", ["property_id"], name: "index_properties_on_property_id", unique: true
add_index "properties", ["user_id"], name: "index_properties_on_user_id"

create_table "users", force: :cascade do |t|
t.string   "name"
t.string   "email"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.string   "password_digest"
t.string   "remember_digest"
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"
end

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

end

数据库:

class Property < ActiveRecord::Base

belongs_to :user, dependent: :destroy 
mount_uploader :picture, PictureUploader
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

has_many :properties

end

模特:

{{1}}

3 个答案:

答案 0 :(得分:0)

如果您能提供routes.rb和错误消息,那就太棒了。

我的猜测是,由于您在属性控制器中执行了@user = User.find(params[:user_id]),因此params中不存在user_id,因此rails无法找到user_id。

routes.rb中,您可能需要

resources :users do 
   resources :properties
end

这样链接就会变成localhost:3000/users/1/properties/new,rails可以找到user_id。

答案 1 :(得分:0)

为什么不使用

@user = User.new(user_permit)

def创建

然后将其添加到控制器的底部

private
def user_permit
    params[:user].permit!
end

答案 2 :(得分:0)

使用此:

UICollectionViewController

这将为current_user创建一个新属性(假设您正在使用Devise进行身份验证)