0

I am using an existing external database which has primary key set has floating/double. All actions work fine except for edit.

The error it gives is this. Is there a way around this or changing it to int is the only way?

No route matches [GET] "/subsystem_tbls/'1.123'/edit"

Routes.rb

Rails.application.routes.draw do
  root "subsystem_tbls#index"
  resources :subsystem_tbls
  resources :ui_types_tbls
  resources :cmd_types_tbls
end

Controller

class SubsystemTblsController < ApplicationController
  def index
    @subs = SUBSYSTEM_TBL.all
  end

  def new
    @subs = SUBSYSTEM_TBL.new
  end

  def show
    @subs = SUBSYSTEM_TBL.find_by(params[:SUBSYSTEM_ID])
  end

  def edit
    @subs = SUBSYSTEM_TBL.find_by(params[:SUBSYSTEM_ID])
  end

  def create
    @subs = SUBSYSTEM_TBL.new(sub_params)
    if @subs.save
      redirect_to @subs
    else
      render 'new'
    end
  end

  def update
    @subs = SUBSYSTEM_TBL.find_by(params[:SUBSYSTEM_ID])
    if @subs.update_attributes(sub_params)
      redirect_to root_url
    else
      render 'edit'
    end
  end

  def destroy
    SUBSYSTEM_TBL.find_by(params[:SUBSYSTEM_ID]).destroy
    redirect_to root_url
  end

  private

  def sub_params
    params.require(:subsystem_tbl).permit(:SUBSYSTEM_ID, :SUBSYSTEM_NAME)
  end
end

Rake ROutes

root_path   GET     /   

subsystem_tbls#index
subsystem_tbls_path     GET     /subsystem_tbls(.:format)   

subsystem_tbls#index
    POST    /subsystem_tbls(.:format)   

subsystem_tbls#create
new_subsystem_tbl_path  GET     /subsystem_tbls/new(.:format)   

subsystem_tbls#new
edit_subsystem_tbl_path     GET     /subsystem_tbls/:id/edit(.:format)  

subsystem_tbls#edit
subsystem_tbl_path  GET     /subsystem_tbls/:id(.:format)   

subsystem_tbls#show
    PATCH   /subsystem_tbls/:id(.:format)   

subsystem_tbls#update
    PUT     /subsystem_tbls/:id(.:format)   

subsystem_tbls#update
    DELETE  /subsystem_tbls/:id(.:format)   

subsystem_tbls#destroy
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
Gunther Gib
  • 117
  • 10
  • 3
    *Why* is your primary key a float? – Jordan Running Feb 20 '18 at 15:34
  • @JordanRunning Thing is its like a version column, ex 1.0023. – Gunther Gib Feb 20 '18 at 15:38
  • And i cant make changes to the database – Gunther Gib Feb 20 '18 at 15:39
  • so i cant eh?.... – Gunther Gib Feb 20 '18 at 15:44
  • It's really unfortunate that you can't change the database, because a float is the wrong data type for a version number, [for several reasons](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate). You want a string, decimal (iif you're certain you'll only have major.minor), or even [multiple integer/string columns](https://github.com/semver/semver/issues/79). You're not totally out of luck, though. I'll write up an answer if no one else beats me to it. – Jordan Running Feb 20 '18 at 15:52
  • What database are you using? – Jordan Running Feb 20 '18 at 16:05
  • 1
    Wow, I can't get my head around this code. Where is `params[:SUBSYSTEM_ID]` defined? It should just be `params[:id]` Can you show your `rake routes` ? – SteveTurczyn Feb 20 '18 at 16:05
  • @JordanRunning I tried changing it to string, still no luck. I'm sure even with the change to a decimal notation i'll encounter the same problem. – Gunther Gib Feb 20 '18 at 16:09
  • @SteveTurczyn I am using an external database with all column and table name in uppercase – Gunther Gib Feb 20 '18 at 16:10
  • @gundergobi that does not mean your routes will generate in this fashion try `params[:id]` instead. If you are unsure about this run rake routes and I would guess you will see routes like "subsystem_tbls/:id" the `:id` portion is what you are referencing – engineersmnky Feb 20 '18 at 16:11
  • @engineersmnky, i have made modifications as you have specified. Yet the same error on edit – Gunther Gib Feb 20 '18 at 16:18
  • And in doing so you have proved @SteveTurczyn and my point `params[:SUBSYSTEM_ID]` should be `params[:id]` now that being said I am unsure why you have a routing issue other than it appears to be placing single quotes into your route. – engineersmnky Feb 20 '18 at 16:21
  • Now its showing this: No route matches [GET] "/subsystem_tbls/1.123/edit" yet if i manually change the 1.123 in the url to 1 its gets into the edit page perfectly. @engineersmnky – Gunther Gib Feb 20 '18 at 16:27
  • @engineersmnky my thoughts exactly. And yea since i'm testing there is only one record as of now. – Gunther Gib Feb 20 '18 at 16:37
  • Yeah I am not sure I can help other than to suggest a schema change maybe @JordanRunning can as mentioned in a comment above – engineersmnky Feb 20 '18 at 16:37
  • I'll check with the schema change with my superiors, and @engineersmnky if you don't mind i have question for you. How hard is it to implement github style search and filtering mechanism in rails and i don't have a clue about it. – Gunther Gib Feb 20 '18 at 16:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165503/discussion-between-engineersmnky-and-gunder-gobi). – engineersmnky Feb 20 '18 at 16:41

1 Answers1

0

Any datatypes which includes a decimal point as a primary key is not suitable for rails.

Gunther Gib
  • 117
  • 10