简体   繁体   中英

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.

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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM