Ruby on Rails for post value of textbox - ruby-on-rails

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

Related

How to manually set a controller variable from a Rails view

I'm trying to enter a URL from the view and pass it into my controller and set it equal to a variable. I'm trying it like this currently but it just keeps returning nil.
This is Controller/posts:
def run
url = params[:url_toget]
puts url
doc = Nokogiri::HTML(open(url))
post = Post.create do |post|
post.body = doc.at_css("body")
post.url_toget = url
end
end
This is Views posts:
<div class="form-group">
<%=text_field :url_toget, class:"form-control", placeholder:"Recipe URL" %>
</div>
<%= link_to "Rake", posts_run_path %>
How can I input a URL and have it pass to equal that variable?
You can't directly set variables from the view to the controller but you can post the request from the view.
Like:
<%= form_tag( '/run' ) do %>
<%= text_field_tag 'my_url' %>
<%= submit_tag %>
<% end %>
Then in your controller run action access the parameter: p params[:my_url]
Remember to enable the POST method for the run action in routes: post 'run', to: 'your_controller#run'
In my case I needed to name a form based on the Controller Action.
The way I did it was to do the following:
This is in HAML
%h2 #{params[:action].capitalize} Branch
If you wanted to do it in HTML you would do the following:
<h2>#{params[:action].capitalize} Branch</h2>
This resulted in the form being named Edit Branch when I was editing and New Branch when I was using the NEW action etc.
I hope this helps someone else.
Scott

Rails 4 - Passing user input back to view

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

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.

Rails: Why isn't the 'create' action saving the newly created Quiz instance?

My form gets passed a 'new' Quiz (not saved to the database). My form partial looks like this:
<%= form_for(#quiz) do |f| %>
<p>
<%= f.check_box(:answer1) %>
<%= f.check_box(:answer2) %>
<%= f.check_box(:answer3) %>
<%= f.check_box(:answer4) %>
<%= f.check_box(:answer5) %>
<%= f.check_box(:answer6) %>
<%= f.check_box(:answer7) %>
<%= f.check_box(:answer8) %>
</p>
<p>
<%= f.submit("Get my results!") %>
</p>
<% end %>
Here is my QuizzesController#create action:
def create
#results = Quiz.create(post_params) #from private method
if #results.save
redirect_to results_path
else
#error handle here
end
end
...which gets triggered when the user clicks 'get my results' on my quiz form. And the post_params method looks like this:
def post_params
params.require(:quiz).permit(:id, :user_id, :answer1, :answer2, :answer3, :answer4, :answer5, :answer6, :answer7, :answer8) #add other attributes here
end
My results/index.html.erb looks like this:
<div class="container">
<!-- Example row of columns -->
<div class="row">
<h1>Results</h1>
<p><%= #results.inspect %></p>
</div>
</div>
But that 'inspected' Quiz instance returns 'nil' for all the answers1, answers2 etc attributes. Any idea why that would be? Is there something I'm NOT doing to save the user's answers to the database?
The reason it shows nil is because you are not setting the variable.
After creating and saving, you redirect to results_path and the variable #results does not persist during a redirect. Without seeing the full code, I'll have to guess at your naming conventions but there are two ways to do this.
1) If you want to redirect to the index then in the code for your index action, you can set the variable:
#results = Quiz.last
This is easy to work with in development because you are the only user and this will always return the last quiz you created. Not so great in production.
2) The alternative is to redirect to the show action for that quiz.
def create
#results = Quiz.new(post_params)
if #results.save
redirect_to result_path(#results)
else
# error handle here
end
end
Again, I have had to guess that result_path is the correct path. Without seeing the full routes file, I cannot be sure but you can rename accordingly if necessary.

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