Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 铁路协会';无';_Ruby On Rails 4_Associations - Fatal编程技术网

Ruby on rails 4 铁路协会';无';

Ruby on rails 4 铁路协会';无';,ruby-on-rails-4,associations,Ruby On Rails 4,Associations,我是Rails和Web开发的新手,真的很困惑。我有两种型号user和property。除非您是用户(已登录),否则无法创建属性。我在关联方面遇到了问题,因为我在用户模型中有很多属性并且属于属性模型中的用户。创建属性时,当我检查控制台时,它具有正确的用户id 问题:当我在控制台中检查用户时,会收到消息property\u id:nil。有人能解释一下我需要什么代码才能为用户填充属性id?(我认为这可能与在属性之前创建的用户有关,但我认为关联会自动处理此问题) 我正在使用designe,以防这是一个

我是Rails和Web开发的新手,真的很困惑。我有两种型号
user
property
。除非您是用户(已登录),否则无法创建属性。我在关联方面遇到了问题,因为我在
用户
模型中
有很多属性
并且
属于
属性
模型中的用户。创建属性时,当我检查控制台时,它具有正确的
用户id

问题:当我在控制台中检查用户时,会收到消息
property\u id:nil
。有人能解释一下我需要什么代码才能为
用户填充
属性id
?(我认为这可能与在属性之前创建的用户有关,但我认为关联会自动处理此问题)

我正在使用
designe
,以防这是一个因素,并将
:property\u id
添加到允许的参数方法中

相关代码如下:

型号:

class Property < ActiveRecord::Base

belongs_to :user, dependent: :destroy
validates :user_id, presence: true 
mount_uploader :picture, PictureUploader
end
class属性
(二)

class用户
控制器:

class PropertiesController < ApplicationController

before_action :authenticate_user!, except: [:index]
before_action :set_user, only: [:show, :edit, :update]


def index
    @properties = Property.all
end

def new 
    @property = current_user.properties.new
end

def create
    @property = current_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 update      
    respond_to do |format|
      if @property.update(property_params)
        format.html { redirect_to @property, notice: "You've successfully updated your property listing!" }
        format.json { render :show, status: :ok, location: @property }
      else
        format.html { render :edit }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
end

end
类属性控制器
(二)

class UsersController
应用程序控制器:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?


protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email,       :password, :password_confirmation, :property_id) }
end
class ApplicationController
您所做的是错误的,属性id将不会存储在用户表中。 你有一对多的关系。如果一个用户有3个属性,那么您将在该单用户记录中存储3个属性id。 不,除了您在代码中所做的正确之外,属性表将具有用户id,并且通过在控制器中执行此操作,您将获得特定用户的所有属性

@user = User.find(:user_id)
@properties = @user.properties

感谢您的反馈。再看一遍就明白了。我学的很慢,但很有把握
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?


protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email,       :password, :password_confirmation, :property_id) }
end
@user = User.find(:user_id)
@properties = @user.properties