Error in the route of a form

0

I have the following error in rails when generating a form:

  

No route matches {: action = > "elec_config",: controller = > "atoms"}   missing required keys: [: query_val]

I have a model called Atom with 3 fields in the database and it adds an attribute in its class in the following way:

    class Atom < ApplicationRecord
      def initialize(*args)
        @query_val = args[0]
      end
    end

Create the following route

get "/atoms/:query_val/elec_config", to: "atoms#elec_config", as: "elec_config"

for the elec_config method in which I do the following:

def elec_config
  @atom = Atom.where("id = :id or atom_name = :atom_name or symbol = :symbol", { id: params[:query_val], atom_name: params[:query_val], symbol: params[:query_val]})
  render plain: @atom.inspect ##solo para saber que este respondiendo bien
end

and when I generate the form to make the query it gives me the error already mentioned.

This is the form

<%= form_for :atom, elec_config_path do  |f|  %>
  <%= f.label :query_val %>
  <%= f.text_field :query_val %>
<% end %>

I would really appreciate your help. Thanks:)

Something else: how can I access the "id" field that returns this @num = Atom.where("id = :id or atom_name = :atom_name or symbol = :symbol", { id: params[:query_val], atom_name: params[:query_val], symbol: params[:query_val]}) and convert it to fixnum?

    
asked by Leonardo Arellano 05.11.2017 в 16:18
source

1 answer

0

When using the path /atoms/:query_val/elec_config you are indicating that you will receive the value of query_val as part of the route, for example:

/atoms/valor_query_val/elec_config

However in your form you are passing it as a parameter POST , not on the route, which receives nil instead.

I see two alternatives you could use:

  • Modify the path to be /atoms/elec_config and your form will work correctly as it is.

  • Use a link instead of a way where, through Javascript you can read the field value and pass it as a variable to helper of routes.

  • The first option is the simplest, so I would recommend you to lean on it.

        
    answered by 05.11.2017 / 17:03
    source