0

I'm quiet new in ruby on rails. I'm trying to develop an app which uses ActiveModel objects only in controllers without saving. Unfortunately after clicking the submit button I get an error. The aim of this app is to perform some calculations and show the results. How can I do that?

Routing Error No route matches [POST] "/shugarcalc"

app/models/sugar_amount.rb

    class SugarAmount   
      include ActiveModel::Model  

      attr_accessor :l, :h, :b, :tw, :tf  

      VALID_NUMBER_REGEX= /[0-9]{1,2}([,][0-9]{1,2})?/  

      validates :l, presence: true, format: { with: VALID_NUMBER_REGEX },  length: { maximum: 10 }  
      validates :h, presence: true, format: { with: VALID_NUMBER_REGEX },  length: { maximum: 10 }  

    end  

config/routes.rb

    SimpleDesign::Application.routes.draw do  
      root 'static_pages#home'  

      resources :sugar_amount, only: [:new, :create]  

      match '/shugarcalc',  to: 'shugar_amounts#shugar',            via: 'get'    

    end  

app/controllers/shugar_amounts_controller.rb

    class ShugarAmountsController < ApplicationController  

      def sugar  

        @sugar_amount=SugarAmount.new  

      end  

      def create    
        @sugar_amount = SugarAmount.new(params[:sugar_amount])    
        /here i want to use some functions /       
        redirect_to root_url      
        /this is temporary, just to see if anything happens  /      

      end  

    end  

app/views/sugar_amounts/sugar.html.erb

    <%= form_for(@sugar_amount) do |f| %>  

      <%= f.label :l, "eggs" %>  
      <%= f.text_field :l %><br>  

      <%= f.label :h, "flour [mm]" %>  
      <%= f.text_field :h %><br>  

      <%= f.submit "Reduce!" %>  
    <% end %>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
Thom
  • 11
  • 2

1 Answers1

1

Your error message:

Routing Error No route matches [POST] "/shugarcalc"

indicates that you are doing a POST to /shugarcalc. But your route is defined for GET requests:

match '/shugarcalc',  to: 'shugar_amounts#shugar',            via: 'get'
# ------------------------------------------------------------^^^^^^^^^^

so a POST to /shugarcalc won't work. Also, that route will be looking for ShugarAmountsController#shugar but you don't have a shugar method in that controller.

First decide if /shugarcalc should be a GET or a POST (hint: if it is just doing some computations and returning a result without modifying anything then it is probably a GET) and then adjust the route's via: option or how you're trying to access the route accordingly. Then add the shugar method to ShugarAmountsController.

I'd also recommend that you spell "sugar" correctly. Sometimes having it as "shugar" and sometimes "sugar" will drive you nuts trying to remember which spelling is used in which place and will probably cause some conflicts with the assumptions that Rails likes to make.

mu is too short
  • 426,620
  • 70
  • 833
  • 800