Rails 4 - Passing user input back to view - ruby-on-rails

Rails newbie here looking for some advice.
I have a basic page with a form and a submit button. When the user hits the submit button, I want to display what was inputted back the user on the same page with some appended text. For example, if the user inputs "jack", then underneath the form I want the string "hello jack!" to be displayed.
How do i pass what is submitted in my view to the controller, append some text, then display back to the page? Note: I don't want to store the input in a database.
Here's what I've got so far, any guidance would be great!
Routes.rb
Rails.application.routes.draw do
get 'home' => 'pages#index'
end
My Controller
class PagesController < ApplicationController
def home
end
end
My view
<h1> Enter some text </h1>
<%= form_tag("/home", method: "get") do %>
<%= text_field_tag(:q) %>
<%= submit_tag("Submit") %>
<% end %>

This should work:
controller.rb:
class PagesController < ApplicationController
def home
if params[:q].present?
#input = "hello #{params[:q]}!"
end
end
end
form:
<%= form_tag("/home", method: "get") do %>
<%= text_field_tag(:q, #input) %>
<%= submit_tag("Submit") %>
How params works in Rails

Related

How to pass parameters to a controllers in ROR

I want to pass year and month to a controller. When the submit button is pressed, the year and month info should be passed to the action show of the controller foo.
I have edited my form as
<%= form_tag "/test" do %>
<%= month_field_tag :yearMonth %>
<%= submit_tag %>
<% end %>
How should I do to archive this in a view and how to get the passed parameter in foo controller?
Rails 4+ You must have a params.require private def in your controller to permit the variables you define as "okay" to be parsed. Your controller will filter out anything not specifically allowed via the .require.
private
def name_of_controller_params
params.require(:name_of_controller).permit(:yearMonth, :anothervar, :etc)
end
To see the params hash in your controller at the action point, you can insert this debug code into your relevant action. You can use this to see if your param is making it from the form into your controller's action.
def show
flash[:info] = "Params hash #{params}."
other actions...
end
Something like this will work:
<%= form_tag '/test' do %>
<%= month_field_tag 'month_value', '' %>
<%= submit_tag %>
<% end %>
Then inspect params from show action to see if month_value is there.

How to re-route to a static page after a sign-up page email is submitted

I want people to be able to submit their email on the home page and then get redirected to the views/pages/about With the following code when they click submit on their email it takes them to http://localhost:3000/premails and gives the following error:
Routing Error
uninitialized constant PremailsController
I've tried adding code to my controller like this:
if #premail.save
redirect_to :action => :about
end
and other variations but they all give me a bunch of other problems and I haven't been able to figure out the cleanest simple way to do this
This is my pages_controller.rb
class PagesController < ApplicationController
def home
#premail = Premail.new
end
def about
end
end
This is the form in my views/pages/home.html.erb
<div class="container-form">
<%= form_for #premail, html: {class: "form-inline", role: "form"} do |f| %>
<% if #premail.errors.any? %>
<h2><%= pluralize(#premail.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% #premail.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="form-group signup-field">
<%= f.label :email, class:"sr-only" %>
<%= f.email_field :email, class:"form-control signup-input", placeholder:"Enter email" %>
</div>
<div>
<%= f.submit "Get Early Access", class:"btn btn-default signup-button" %>
</div>
<% end %>
</div>
This is my routing:
root 'pages#home'
resources :pages
resources :premails
This is my premail model
class Premail < ActiveRecord::Base
end
This is my migration:
class CreatePremails < ActiveRecord::Migration
def change
create_table :premails do |t|
t.text :email
t.timestamps
end
end
end
What would you change to re-route to the views/pages/about page and be able to retain the email(premail) in your database? Thanks!
you need the premails_controller.rb to make the view interact with the premail model.
Now when you have the premails controller.
resources :premails
will work and form_for #premail will create a form for an individual Premail model object.
you will have to now make the #premail instance variable available here :
views/premails/new.html.erb
by using this :
class PremailsController < ApplicationController
def new
#premail = Premail.new
end
def create
#premail = Premail.create(premail_params)
if #premail.save
redirect_to :action => :about
end
end
def about
render template: "premails/about"
end
private
def premail_params
params.require(:premail).permit(:email)
end
end
make sure you have the about.html.erb page there inside the premails path
The problem is in what your form_for #premails does when submitting it.
Clicking the submit button on a form_for object will try to send the object either to the 'create' or 'update' action it's own controller depending on whether it's a new resource or not.
as the error message shows here you do not have a Premails controller. You either need to create a Premails controller with a create an update action or you need to change where the form sends the submit button to.
If you create the controller than in the create action you can redirect_to the about_page_path like this:
def create
#premail = Premail.new(premail_params)
if #premail.save
redirect_to about_page_path
else
redirect_to :back
end
end
Or if you dont want to create an entire premail controller you can change what the f.submit button does in your form to direct to an action in your pages controller. You would have to do something like
button_tag type: "submit"
The preferred method would be to create a premails controller.
the problem is in what your form for premails does sumbitting it. Clicking the submit button on a form_for object will try to send the object either to the create or update action in your controller depending on whether it's a new resource or not.
as the error message shows here you do not have a premails controller. You either need to create a premails controller with a create an update action or you need to change where the form sends the submit button to.
you can do this by changing that f.submit to a button_tag type submit.

Rails controller : How to pass parameters between actions in wizard style?

I'm trying to build a wizard style site to help creating an object.
Each step user inputs some parameters. I want to create and persist the object in DB only when all parameters are submitted at the final step.
class ExperimentsController < ApplicationController
def wizard_step_1
render template: "experiments/wizards/step1"
end
def wizard_step_2
render template: "experiments/wizards/step2"
end
def wizard_step_3
binding.pry
# how to access params in step 1 & 2 here?
end
end
View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
<%= text_field_tag("user_name") %>
<%= submit_tag("Next >>") %>
<% end %>
View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
<%= text_field_tag("user_email") %>
<%= submit_tag("Next >>") %>
<% end %>
I didn't put routes info here as it's tested OK. When user visit step1 and input user name, when submitting form control goes to wizard_step_2 and show view step2. Here user inputs email and when clicking submit button execution goes to action wizard_step_3.
Then my question is how to obtain user name and email the user inputs in previous steps?
I'd thought about gem Wicked, and Rails cache etc., and more or less they don't fulfill my needs. Is there a decent way doing this?
I don't have huge experience with this, but you may be able to benefit from: Wizard Forms Railscast
I'd imagine this type of setup will save the step 1, step 2, etc params as session variables:
class ExperimentsController < ApplicationController
def wizard_step_1
render template: "experiments/wizards/step1"
end
def wizard_step_2
render template: "experiments/wizards/step2"
#Create session vars from step1
session[:user][:name] = params[:user_name]
end
def wizard_step_3
binding.pry
#Step2 is already passed as params ;)
params[:user_name] = session[:user][:name]
end
end
View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
<%= text_field_tag("user_name") %>
<%= submit_tag("Next >>") %>
<% end %>
View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
<%= text_field_tag("user_email") %>
<%= submit_tag("Next >>") %>
<% end %>
I know this example is not very DRY, but it's the basis of what I'd start to look at. Essentially, you need to be able to persist data between instances of objects, which lends itself entirely to sessions

Ruby on Rails for post value of textbox

Can any body tell me how should I get post value from a textbox and pass it to controller putting in if condition but no output is coming
home/index.html.erb
<%text_field_tag (:text1,nil,placeholder,"Enter the username")%>
controller/home_controller.rb
def create
#data = parmas[:text1]
if(#data=="abc")
(login to another page)
end
end
First of all put = sign before text_field_tag. You are using home controller which mostly used for static pages. To get this value you have to put this in form tag like this:
<%= form_tag root_path do %>
<%= text_field_tag (:text1,nil,placeholder,"Enter the username")%>
<%= submit_tag :search %>
<% end %>

Passing Form Values into a controller in Rails

say I have a text field like the following in a view called 'search':
<%= text_field_tag(:lookup) %>
how do I submit this ':lookup' value and pass it into the controller called 'search' and assign it to a variable?
It's a basic problem, but being a noob, it's difficult ;)
That will be accessible in the controller as
params[:lookup]
Your controller could look something like this:
class SearchesController < ActionController::Base
def search
lookup = params[:lookup]
#models = Model.find_by_lookup(lookup)
end
end
And your view should look like this:
<%= form_tag searches_path do %>
<label for="lookup">Lookup</label>
<%= text_field_tag :lookup %>
<%= submit_tag "Submit" %>
<% end %>

Resources