JustusP
JustusP

Reputation: 19

Get form data in controller rails

I'm fairly new to rails and didin't find a soloution after a lot of research...

I want to build a simple data abstaraction test app. I have a text field and a submit button and I want to do something with the input (like ie. .upcase ) and then print it out. On submit, I get a routing error, though.

What am I doing wrong?

application.html.erb

<body>
<%= form_tag("parse_input", method: "post") do %>
<%= text_field(:ans, :field) %>
<%= submit_tag("submit" %>
<% end %>

FormControllerController.rb

class FormControllerController < ApplicationController
def parse_input
params[:ans].each do |value|
puts value
end
end
end

routes.rb

Rails.application.routes.draw do
...
root :to => 'application#home'
get "application" => "form_controller_controller"
end

I don't want to use a DB btw.

Upvotes: 0

Views: 3161

Answers (1)

puneet18
puneet18

Reputation: 4427

Try below code:

routes.rb

Rails.application.routes.draw do
...
  root :to => 'application#home'
  post "parse_input" => "form_controller_controller#parse_input"
end

application.html.erb

<body>
<%= form_tag("/parse_input") do %>
  <%= text_field(:ans, :field) %>
  <%= submit_tag("submit") %>
<% end %>

Upvotes: 1

Related Questions