Wednesday, January 25, 2017

Rails Simple Form Update a Div based on Select | Fixed issues

Newest questions tagged ruby-on-rails - Stack Overflow

Rails Simple Form Update a Div based on Select | Fixed issues


Rails Simple Form Update a Div based on Select

Posted: 25 Jan 2017 07:40 AM PST

I'm trying to update a text in a div with the value that the user selects in a dropdown list, using Simple Form on Rails5. It should work like this, but I can't get it to work in Simple Form: http://jsfiddle.net/zwzakdnv/

= f.input :service_name, collection: @my_services, label_method: :service_name, value_method: :service_name, id: "service"    #service_will_show_here    javascript:  $('#service').change(function(){    $selected_value=$('#service option:selected').text();    $('#service_will_show_here').text($selected_value);  });  

how to access class method from instance method

Posted: 25 Jan 2017 07:41 AM PST

I have model:

class UserWeight < ActiveRecord::Base      self.table_name = 'Weight'      scope :logged_between, lambda { |start_date, end_date|      where(weight_date: start_date.to_date...(end_date.to_date.end_of_day)) unless      start_date.nil? || end_date.nil?    }      # returns the sum of points     def total_points        self.class.Userweight      (UserWeight.blank?) ? 0 : UserWeight.inject(0) {|a, e| a + e.points}    end  end  

Active record is returning collection of arrays. I want to access class method and do the sum in the instance method. Any idea how can i do that?

Thanks

RecordNotFound using Capybara with Poltergeist

Posted: 25 Jan 2017 07:38 AM PST

I'm getting a RecordNotFound exception when trying to visit a page with Capybara, and it is only happening with js: true (using the Poltergeist driver).

My feature spec looks like this:

  context 'existing submissions' do      background do        @problem = create(:problem)        @input = create(:input, problem: @problem)      end      scenario 'goes back and edits', js: true do        visit "/problems/#{@problem.id}/#{@input.lens}"  

And when I drop down into byebug I am seeing this:

[27, 36] in /Users/danielklasson/third_space/spec/features/user_completes_solving_process_spec.rb     30:     scenario 'goes back and edits', js: true do     31:       byebug  => 32:       visit "/problems/#{@problem.id}/#{@input.lens}"  (byebug) visit "/problems/#{@problem.id}/#{@input.lens}"  {"status"=>"success"}  (byebug)  *** ActiveRecord::RecordNotFound Exception: Couldn't find Problem with 'id'=1    nil  (byebug) Problem.all  #<ActiveRecord::Relation [#<Problem id: 1, name: "My Problem", created_at: "2017-01-25 15:35:12", updated_at: "2017-01-25 15:35:12">]>  

Rails change primary id to 64 bit bigint

Posted: 25 Jan 2017 07:44 AM PST

I am using rails and the mysql2 adapter. I want to change all primary ids and foreign keys to be 64 bit integers instead of the default 32 bit as they are right now for my production database.

Is this possible on the fly or do I have to drop the database, change the structure and import the data again?

If there is a way to do it without dropping the database, even if it's a hack, it would be great to know.

Rails creating duplicates when editing nested form

Posted: 25 Jan 2017 07:31 AM PST

I did a lot of googling, and all come back to adding :id to the permitted attributes, but that doesn't work in my case so: How can i prevent multiple empty rows (in related tables)from being created when editing a nested form? Every time i eddit a form, it doubles the amount of empty related records.

The controller:

    class EnquiriesController < ApplicationController    before_action :authenticate_user!    load_and_authorize_resource    # before_action :set_enquiry, only: [:show, :edit, :update, :destroy]    #11-1-2017 Devise geinstalleerd, nog verder afmaken!(oa onderstaande uncommenten)        # GET /enquiries    # GET /enquiries.json    def index     # @enquiries = Enquiry.all      @enquirie = current_user.enquiries    end      # GET /enquiries/1    # GET /enquiries/1.json    def show    end      # GET /enquiries/new    def new      @enquiry = Enquiry.new      @enquiry.enquirymeasures.build         #@enquiry.measurements.build     @enquiry.tools.build      @enquiry.build_applicant      @enquiry.signatures.build      @enquiry.gasmeters.build         #@enquiry.enquirymeasures.build.build_measurement    end      # GET /enquiries/1/edit    def edit      #nog op kunnen slaan!!!! 1-01-17 Marco      @enquiry = Enquiry.find(params[:id])      @enquiry.enquirymeasures.build        @enquiry.tools.build      @enquiry.build_applicant      @enquiry.signatures.build     @enquiry.gasmeters.build      end      # POST /enquiries    # POST /enquiries.json    def create      @enquiry.user_id = current_user.id      @enquiry = Enquiry.new(enquiry_params)      #@enquiry.enquirymeasures.build        respond_to do |format|        if @enquiry.save          format.html { redirect_to @enquiry, notice: 'Enquiry was successfully created.' }          format.json { render :show, status: :created, location: @enquiry }        else          format.html { render :new }          format.json { render json: @enquiry.errors, status: :unprocessable_entity }        end      end    end      #approved_enquiry_notification      # PATCH/PUT /enquiries/1    # PATCH/PUT /enquiries/1.json    def update      @enquiry = Enquiry.find(params[:id])       respond_to do |format|        if @enquiry.update(enquiry_params)          format.html { redirect_to @enquiry, notice: 'Enquiry was successfully updated.' }          format.json { render :show, status: :ok, location: @enquiry }        else          format.html { render :edit }          format.json { render json: @enquiry.errors, status: :unprocessable_entity }        end      end    end      # DELETE /enquiries/1    # DELETE /enquiries/1.json    def destroy      @enquiry.destroy      respond_to do |format|        format.html { redirect_to enquiries_url, notice: 'Enquiry was successfully destroyed.' }        format.json { head :no_content }      end    end       private      # Use callbacks to share common setup or constraints between actions.      def set_enquiry        @enquiry = Enquiry.find(params[:id])      end        # Never trust parameters from the scary internet, only allow the white list through.    #25-1 MG alle attributes hadden ook :id      def enquiry_params        params.require(:enquiry).permit(:id, :reference, :location, :description, :date, :amount, :approved, enquirymeasures_attributes: [:id, :responsible, :done, :needed, :measurement_id, :user],                                        tools_attributes: [:id, :handtool, :other, :motorvehicle, :compressor, :ramp, :scaffold, :crane, :ladder, :generator, :tankladder],                                          applicant_attributes: [:id, :name, :email, :contractor_id],                                        signatures_attributes: [:id, :date, :signature, :representative_id],                                        gasmeters_attributes: [:id, :date, :tester, :signature, :oxigen, :o_needed, :o_continu, :explosives, :e_needed, :e_continu, :mat1, :mat1_needed, :mat1_continu, :mat2, :mat2_needed, :mat2_continu, :mat3, :mat3_needed, :mat3_continu],                                        controls_attributes: [:id, :enquirycheck, :workspacecheck, :enquiry_id]).merge(user_id: current_user.id)          end        end  

The models: main:

class Enquiry < ActiveRecord::Base    has_many :enquirymeasures, dependent: :destroy    accepts_nested_attributes_for :enquirymeasures, :reject_if => lambda { |a| a[:responsible].blank? }, :allow_destroy => true        has_many :tools, dependent: :destroy    accepts_nested_attributes_for :tools      has_many :controls, dependent: :destroy    accepts_nested_attributes_for :controls      has_one :applicant, dependent: :destroy    accepts_nested_attributes_for :applicant      has_one :contractor, through: :applicant      has_many :signatures, dependent: :destroy    accepts_nested_attributes_for :signatures    has_many :representatives , through: :signatures, :source => :representatives      has_many :gasmeters, dependent: :destroy    accepts_nested_attributes_for :gasmeters      belongs_to :user      #voor de goedkeuring mail    after_create :send_confirmation_mail    def send_confirmation_mail      ModelMailer.new_enquiry_mail(self).deliver    end      #after_update :send_approved_mail    #def send_approved_mail     # if params[:approved] == '1'    #  if :approved == '1'    #  ModelMailer.enquiry_approved_mailer(self).deliver    #  end    

3 comments:

  1. Nice post ! Thanks for sharing valuable information with us. Keep sharing.. Ruby on Rails Online Training Bangalore

    ReplyDelete
  2. Thanks For sharing your information!!!
    Germany VPS Server Hosting

    ReplyDelete
  3. Wonderful blog posting!!! thanks for sharing your great information..
    Germany VPS Server Hosting

    ReplyDelete