Ruby on rails 如何在Rails中保存关联的id?

Ruby on rails 如何在Rails中保存关联的id?,ruby-on-rails,ruby,Ruby On Rails,Ruby,batchu controller.rb user.rb _form.html.erb 我有两个属于的模特,有很多协会。这里没有在Batchnotification模型中保存用户id,请告诉我如何存储用户id的详细过程。请在问题中发布表单代码。我发布了请检查一下,只是猜测一下。您正在使用不提供用户ID的参数创建新对象。不知道新对象应属于哪个用户,但您可以尝试在@batch\u notification=BatchNotification.newbatch\u notification\u参数行后

batchu controller.rb

user.rb

_form.html.erb


我有两个属于的模特,有很多协会。这里没有在Batchnotification模型中保存用户id,请告诉我如何存储用户id的详细过程。

请在问题中发布表单代码。我发布了请检查一下,只是猜测一下。您正在使用不提供用户ID的参数创建新对象。不知道新对象应属于哪个用户,但您可以尝试在@batch\u notification=BatchNotification.newbatch\u notification\u参数行后设置用户ID字段。请注意,这只是为了调试目的。
 class BatchNotificationsController < ApplicationController
  before_action :set_batch_notification, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @batch_notification = BatchNotification.new
    @users = User.all


    @batch_notifications = BatchNotification.all
    @final_count = []
    @calculated_batch_counts = CalculatedBatchCount.all.group_by{|x| x.batch.batch_number  if !x.batch.nil? }
    @a = CalculatedBatchCount.all.group_by{|k| k.batch.serial_id if !k.batch.nil? }


    @calculated_batch_counts.each do |key, values|
      count = values.map{|x| x.finalCount}.length
      h = {"batch_number" => key, "batch_id" => values.map{|x| x.batch.serial_id},"finalcount" => values.map{|x| x.finalCount}.sum(:+)/count}
      @final_count << h
    end


    puts
    # => render :json => @final_count and return
    respond_with(@batch_notifications)
  end


  def show
    respond_with(@batch_notification)
  end

  def new
    @batch_notification = BatchNotification.new


    respond_with(@batch_notification)
  end

  def edit
  end

  def create
    @batch_notification = BatchNotification.new(batch_notification_params)


    respond_to do |format|
      if @batch_notification.save
        format.html { redirect_to batch_notifications_path, notice: 'batch_notification was successfully created.' }
        format.json { render action: 'index', status: :created, location: @batch_notification }
        format.js
      else
        format.js
        format.html { render action: 'new' }
        format.json { render json: @batch_notification.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
        @batch_notification.update(batch_notification_params)

    respond_to do |format|

      if @vehicle.update(vehicle_params)
        format.html { redirect_to @batch_notification, notice: 'batch_notification was successfully updated.' }
        format.json { head :no_content }
        format.js
      else
        format.js
        format.html { render action: 'edit' }
        format.json { render json: @batch_notification.errors, status: :unprocessable_entity }
      end
    end
  end
  def destroy
    @batch_notification.destroy
    respond_with(@batch_notification)
  end

  private
    def set_batch_notification
      @batch_notification = BatchNotification.find(params[:id])
    end

    def batch_notification_params
      params.require(:batch_notification).permit(:message,:approved,:finalCount, :batch_id, :user_id)
    end
end
   class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource

  # GET /users
  # GET /users.json
  def index
    @users = User.all.order('created_at DESC')
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create

    respond_to do |format|
      if @user.save
        format.html { redirect_to users_path, notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
        format.js
      else
        # render :text =>  @user.errors.inspect and return
        format.html { redirect_to users_path, notice: 'Erors while creating User'}
        format.json { render json: @user.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  # def update
  #   respond_to do |format|
  #     if @user.update(user_params)
  #       format.html { redirect_to @user, notice: 'User was successfully updated.' }
  #       format.json { head :no_content }
  #     else
  #       format.html { render action: 'edit' }
  #       format.json { render json: @user.errors, status: :unprocessable_entity }
  #     end
  #   end
  # end
  def update
    if user_params[:password].blank?
      user_params.delete(:password)
      user_params.delete(:password_confirmation)
    end
    params[:user][:name] = params[:user][:name].capitalize if !params[:user][:name].nil?

    successfully_updated = if needs_password?(@user, user_params)
                             @user.update(user_params)
                           else
                             @user.update_without_password(user_params)
                           end

    respond_to do |format|

      if successfully_updated
        format.html { redirect_to users_path, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end


  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
      format.js { render :layout => false}
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation, :name, :role_id,:department_id,:encrypted_password, :plant_id)
    end
    protected
    def needs_password?(user, params)
      params[:password].present?
    end

end
    class BatchNotification
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Autoinc

  field :finalCount, type: Float
  field :message, type: String
  field :approved, type: Boolean
  field :batch_id, type: Integer
  field :user_id, type:Integer




  belongs_to :batch
  belongs_to :user
  belongs_to :calculated_batch_counts

end
class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include DeviseTokenAuth::Concerns::User




  # field :locked_at,       type: Time
  field :name, type: String
  field :role_id , type: Integer
  field :department_id , type: Integer

  ## unique oauth id
  field :provider, type: String
  field :uid, default: ""



  belongs_to :role
  belongs_to :department
  belongs_to :plant

  has_and_belongs_to_many :batches, :dependent => :destroy

  has_many  :batch_notifiations , :dependent => :destroy




end
    <%= simple_form_for(@batch_notification)  do |f| %>
    <%= f.error_notification %>
    <%= f.check_box :approved, label: false%>
    <%= f.input :message, label: false, placeholder:"message"%>


   <%= f.submit "Add", class: "btn btn-primary" %>
<% end %>