getting form value into controller in ruby on rails - ruby-on-rails

I have a "search" page that have some controls and below is the search page code:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
</table>
</td>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">
Result
</h2>
<% #user_zip.each do |uzr_zip| %>
<h1><%= uzr_zip.First_Name %></h1>
<% end %>
<table id="searchResult" width="100%" runat="server">
<tr>
<td bgcolor="#CCDBE0">
Image:
</td>
<td bgcolor="#CCDBE0">
<%= f.label(:zip, "Mentor") %>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% end %>
And when I am trying to get the textbox value into the controllers page like below
def search
#students=Students.all
#blah = params[:search][:tf_Zip]
end
render 'search'
end
Then it gave me an error below, at this line #blah = params[:search][:tf_Zip]
undefined method `[]' for nil:NilClass
Kindle help me. Thanks

I think your params[:search] is nil?
so for this you can use
#blah = params[:tf_Zip]
or change your input field like this
<%= f.text_field :tf_Zip %>
or you can use like this
<input type="text" name="search[tf_Zip]" Style="text-align: left;"
BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">

Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.
The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.
Your problem is this line:
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].
What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
can be replaced with
<%= f.text_field :tf_Zip %>
which will populate params[:search][:tf_Zip]
For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.

Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.
Usually you'd submit the form from one controller action and handle the result in another action.
And your statement end looks misplaced.

Related

Rails search with will Paginate

Below the code I am using now.Current code pagination will show 100 users per page.Currently search functionlaity implemented using JQuery code.Since pagination showing 100 records, search only working for 100 records. I need to implement search with will paginate.If I search users,It will fetch details from database and show in table .Can anyone please suggest how implement this with will paginate in rails?
Controller
#user_list = User.all.paginate(:page => params[:page], :per_page => 100)
html.erb
<%= will_paginate #user_list %>
<table id="users_access_table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Curernt Access</th>
<th>API User Access</th>
</tr>
</thead>
<tbody id="table_body">
<br>
<div class="col-md-4" style="float: left;">
<input class="form-control" title="Search" type="text" id="userinput" onkeyup=" searchUsers()" placeholder="Search 🔍 :">
</div>
<br>
<br>
<% #user_list.each do |user| %>
<tr>
<td>
<%=user['name']%>
</td>
<td>
<%=user['email']%>
</td>
<td>
<%=user['access']%>
</td>
<td>
<%=user['api_users']%>
</td>
</td>
<td>
You need to make changes in the controller level and UI side as well.
I am considering the parameters are coming to the controller from the UI side, and making changes in the query on the user controller.
Instead of all you need to create LIKE query
#user_list = User.all.paginate(:page => params[:page], :per_page => 100)
change it with
#user_list = User.where("name LIKE ? OR email LIKE ?", "%#{search_param}%", "%#{search_param}%").page(params[:page]).per(100)
hope it helps you to resolve your query.

SET ASP.NET MVC4 button value

I'm a newbie.
My problem is that i have a button and i want to set the value by a ASP.NET variable.
<input type="button" id="mybt" value="<%=Student.Name %>" />
This code is not working.
The code:
<%var Products = (IEnumerable<Demo.Models.Product>)ViewBag.Products;%></div>
<div>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Price</td>
</tr>
<%foreach(var product in Products)
{ %>
<tr>
<td style="width:68px;height:20px">
<%=product.Id %>
</td>
<td style="width:120px">
<%=product.Name%>
</td>
<td style="width:120px">
<%=product.Price %>
</td>
<td style="width:120px">
<input type="button" value='<%=product.Id%>' />
<%} %>
</td>
</tr>
Instead of <%=Student.Name %> use this: #Student.Name
Provided that "Student" is the model name and "Name" is the property you want.
The reason for this is because <%= %> works for aspx but not the razor view engine which is probably what you have chosen for your MVC solution.

How to clear all tha data each time the app is accessed?

I actually want to my database entries to be empty each time the app is accessed. But i am not getting any idea regarding this. Below is my code which takes 3 inputs from user and displays them in tabular form but each time I display the table old entries are also getting displayed. I want that once all the entries are displayed the database should get cleared.
<p>enter the name with registration number of students</p>
<form accept-charset="UTF-8" action="/pages/add" method="post">
<input id="nam" name="nam" type="text">
<input id="reg" name="reg" type="text">
<input id="cls" name="cls" type="text">
<input name="commit" type="submit" class="btn" value="Add todo">
</form>
<table width="100%">
<tr>
<th>NAME</th>
<th>REGISTRATION NUMBER</th>
<th>CLASSES ATTENDED</th>
</tr>
<tr>
<% #pages.each do |t| %>
<td><%= t.name_student %></td>
<td><%= t.reg_no %></td>
<td><%= t.classes_at %></td>
</tr>
<% end %>
</table>

form is not ended properly inside the table that causes update method

Here is my code, i have wasted lot of time on this but still did not get the answer.Form ended in the same row where it started and because of this submit method is not adding the checkbox values in database.please help?
< table>
<% pos = 0%>
<% #accounts.each do |myacc| %>
<% if #accounts.length == pos +1 %>
<tr class="last">
<% else%>
<tr>
<% end %> <!-- end of if tag -->
<td class="position"><%= pos += 1%>.</td>
<td ><%= myacc.name %>< /td>
< % form_for(:account,:html =>{:method =>:put,:id =>"editAccountForm_"+pos.to_s,:multipart => true}) do|f|%>
<td>
<% if myacc.place_matching == "1" %>
<input type="checkbox" id=place_matching value=1 checked="checked" />
<% else %>
<input type="checkbox" id=place_matching value=0 />
<% end %> <!-- end of if tag -->
</td>
<td>
<% if myacc.data_pulling == "1" %>
<input type="checkbox" id="data_pulling" value=1 checked="checked" />
<% else %>
<input type="checkbox" id="data_pulling" value=0 />
<% end %> <!-- end of if tag -->
</td>
<td>
<a class="gray-button gray-whitebg" href="#" onclick="getElementById('editAccountForm_<%= pos%>').submit();return false;" />ADD</a>
</td>
<% end %> <!-- end of form tag -->
</tr>
<% end %>
</table>
In rails 3 you always have to use <%= %> form, also for blocks.
So write
<%= form_for ...
I see you use the form_for, but then not use f at all. Why not use f.check_box ? See documentation.
Or use form_tag and then use check_box_tag. See documentation.
You should be using more of the helpers rails gives you.
Please paste your complete form..
please take you form out from your table..
hope this will help

Change table row using Rails Ajax

I've a simple page with link_to_remote Rails Ajax function and HTML table.
I'd like to change row of the table when click that link.
This is my html.
<table border="1">
<tr>
<td><div id="ajax_result_1">1</div></td>
<div id="ajax_result_2"><td>2</td></div>
</tr>
<div id="ajax_result_3">
<tr>
<td>3</td>
<td>4</td>
</tr>
</div>
</table>
And this is my code.
<%= link_to_remote 'Change', :update => "ajax_result_1", :url => "change_path" %>
change action just render simple text.
When I use ajax_result_1 for :update, it worked okay.
But, not for ajax_result_2 and ajax_result_3.
Is there a way to solve this? I want to replace row of the table.
<div id="ajax_result_2"><td>2</td></div>
this should be
<td><div id="ajax_result_2">2</div></td>
and
<div id="ajax_result_3">
<tr>
<td>3</td>
<td>4</td>
</tr>
</div>
should be
<tr id="ajax_result_3">
<td>3</td>
<td>4</td>
</tr>
you cann't use <div> tag in table directly if you want to use <div> you have to use it in the <td> only.

Resources