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

时间:2015-09-14 09:20:45

标签: ruby-on-rails ruby

batchnotification_controller.rb

 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

user_controller.rb

   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

batchnotification.rb

    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

user.rb

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

_form.html.erb

    <%= 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 %>

我有两个属于has_many关联的模型。这里不保存Batchnotification模型中的User_id,请告诉我如何存储用户ID的详细步骤。

0 个答案:

没有答案