I am building an admin section for an app I'm working that lists businesses. Admins can then go in and submit an email address and activate the business. So I am using a form_tag within a block like so:
<% for business in #businesses %>
<tr>
<td align="center" class="border-table"><%= business.id %></td>
<td align="center" class="border-table"><%= business.name %></td>
<td align="center" class="border-table"><%= business.address %></td>
<td align="center" class="border-table"><%= business.phone %></td>
<% #user = User.new %>
<%= form_tag "/businesses/activate?business_id=#{business.id}", :remote => true, :method => :put do %>
<td align="center" class="border-table" id="<%= business.id %>_email"><%= text_field_tag "email", nil, :placeholder => "email" %></td>
<td align="center" class="border-table" id="<%= business.id %>_activate"><%= submit_tag "Activate" %></td>
<% end %>
</tr>
<% end %>
So on a given admin page there are 25 of these forms, one for each row in the table.
The problem is, for whatever reason, the "email" param is not getting posted, just the business_id (from the path).
Is there something I am doing wrong? Are you not supposed to generate multiple similar forms using a block?
Any advice would be much appreciated.
Thanks!
i figured it out. it seems most browsers won't pass the params of a form unless it's inside the <td> like so:
<td align="center" class="border-table" id="<%= rep.id %>_email">
<%= form_tag activate_rep_path(rep.id), :remote => true do %>
<%= text_field_tag "email", nil, :placeholder => "email", :index => rep.id %></td>
<td align="center" class="border-table" id="<%= rep.id %>_activate">
<%= submit_tag "Activate" %>
<% end %>
</td>
Related
Basically I created a controller, model and view for subjects. Basically I have 6 actions inside my controller and set up a REST inside my routes file to route the right file.
When I entered, http://localhost:3000/subjects/index it shows me the view for show.html.erb instead of the index.html.erb
Here's what my subject controller looks like:
class SubjectsController < ApplicationController
def index
#subjects = Subject.sorted
end
And here's the content of my index.html.erb file.
<% #page_title = "All Subjects" %>
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", new_subject_path, :class => "action_new") %>
<table class="listing" summary="Subject list" border="1">
<tr class="header">
<th>#</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %> </td>
<td><%= subject.name %> </td>
<td class="center"><%= status_tag(subject.visible) %></td>
<td class="center"><%= subject.pages.size %> </td>
<td class="actions">
<%= link_to("View Pages", pages_path(:subject_id => subject.id), :class => 'action show') %>
<%= link_to("Show", subject_path(subject), :class => 'action show') %>
<%= link_to("Edit", edit_subject_path(subject), :class => 'action edit') %>
<%= link_to("Delete", delete_subject_path(subject), :class => 'action delete') %>
<td>
</tr>
<% end %>
<table>
</div>
Also here's what I set up on my routes:
resources :subjects do
member do
get :delete
end
end
Any idea what am I missing?
The answer is simple: in order you access the index page, you need to hit the following URL:
http://localhost:3000/subjects
Obviously, it will be with GET
The reason why you got the error is: since, anything of the format subjects/:id will take you to the show action within SubjectsController, so Rails interprets subjects/index as you are trying to access the page subjects/:id with index as id of the subject. Here's nothing wrong with Rails, since in RESTful web services, you access the index page by ONLY using the plural name of the resource, like in your case http://localhost:3000/subjects
Form screenshot
the image up explain that there is employ who has been allocated three projects at this time he want to full up the fields as he want example he can fill up 2 fields and press submit btn or 1 or 3 depending how many projects he has been assigned.
what i was unable to do is that i want to insert that multiple fields in a table in single submit i do not understand what code i write it will work
the code in my controller is the following.
def new
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new
#project=Project.where(:employee_id => params[:employee_id]).sorted
end
def create
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new(params.require(:timesheets).permit(:employee_id,:project_id,:IN,:comments))
if #timesheet.save
flash[:notice] = "data entered."
redirect_to(:action => 'show',:employee_id =>#pro.id)
else
flash[:notice]="logged out"
end
end
the code in my new.html.erb is the following
<%= form_for(:timesheets, :url => {:action => 'create',:employee_id => #pro.id}) do |d| %>
<% if !#project.nil? %>
<% #project.each do |page| %>
<tr>
<%= d.hidden_field("employee_id" ,:value => #pro.id) %>
<%= d.hidden_field("project_id" ,:value => page.id) %>
<% if !page.employee_id.blank? %>
<td><%= page.prog_name %></td>
<td><%= d.text_field("IN",:class => "qty1") %></td>
<td><%= d.text_field("comments") %></td>
<% end %>
<% end %>
</tr>
<% end %>
<tr>
<td>Total hours</td>
<td colspan="2"><%= text_field_tag("total")%></td>
</tr>
<tr border="0">
<td ><%= submit_tag("Submit") %></td>
<td colspan="2" border="0"></td>
</tr>
I'm trying to edit a collection of users. I want to render table with a list of names and checkboxes. I'm close, but I'm missing something.
My form looks like so:
<%= form_for 'users[]', :url => approve_users_path, :html => {:class => 'form-horizontal'} do |f| %>
<tbody>
<% for user in #users %>
<%= fields_for user do |u| %>
<tr>
<td><%= user.name %></td>
<td><%= u.check_box :vouched %></td>
</tr>
<% end %>
<% end %>
</tbody>
<% end %>
which generates
<tr>
<td>steve cabillero</td>
<td><input name="user[vouched]" type="hidden" value="0" /><input id="user_vouched" name="user[vouched]" type="checkbox" value="1" /></td>
however, I need the input name in the form of users[id][vouched] and vouched is a virtual attribute.
when I try to use f.check_box I get a method not found error.
you should use form_tag if you are not using a specific resource in the form. I also suggest using #each instead of a for loop and check_box_tag for the checkbox
<%= form_tag approve_users_path, :class => 'form-horizontal' do %>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= check_box_tag "users[#{user.id}][vouched]" %></td>
</tr>
<% end %>
</tbody>
<% end %>
I have the following table which displays the results of a match and the selections for that match for the signed in user. I want to display a text value in the selection.winner and selection.value columns when there is no related record found in the database but I am unsure how to do this. My code is as follows:
<% Fixture.where(:weekno => #gameweek.number).each do |fixture| %>
<tr>
<td width="10"><%= fixture.date.strftime("%d/%m/%Y")%></td>
<td width="10"><%= fixture.date.strftime("%H:%M") %></td>
<td width="80"><%= fixture.home_team %></td>
<td width="10">Vs.</td>
<td width="80"><%= fixture.away_team %></td>
<td width="10"><%= fixture.result %></td>
<% Selection.where(:userid => current_user.id, :fixtureno => fixture.id).each do |selection| %>
<td width="10"><%= selection.winner %></td>
<td width="10"><%= selection.value %></td>
<% end %>
</tr>
<% end %>
Do you want to display any string if the database field is nil??
If yes you can use,
<%= selection.winner || 'string' %>
<%= selection.value || 'string' %>
You can put the results of the Selection lookup into a variable and test against that:
<% user_selections = Selection.where(:userid => current_user.id, :fixtureno => fixture.id) %>
<% if user_selections.empty? %>
<td>You have no selections for this match.</td>
<% else %>
<% user_selections.each do |selection| %>
<td width="10"><%= selection.winner %></td>
<td width="10"><%= selection.value %></td>
<% end %>
<% end %>
I've put a <% and %> on each line because I think it is clearer. You could just use one for each block of code though, for example:
<% else
user_selections.each do |selection| %>
I'm not sure how many user selections you have for a particular fixture but this will find them all so if there's loads you'll get them all which will cause poor performance AND a massive row in your table. You know your data better than me though, maybe there's a limit on user selections.
How do I close a form_tag? Here's my code:
<%= form_tag :action => 'authenticate' %>
<h1>Already a member?</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Login" class="form_button" /></td>
</tr>
</table>
<hr />
<%= form_tag :action => 'register' %>
<h1>Register</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Email*: </td>
<td><%= text_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Register" class="form_button" /></td>
</tr>
</table>
I tried <% end %> and <% end_form_tag %>, but I got errors. (Unexpected kEND). I've Googled around a bit, and nothing I've seen really helps. Oh, if I delete everything after the horizontal ruler, the form works fine. But I'd like to have two forms on the page...
I'm using Rails 2.3.5.
form_tag also takes a block, inside which you can put the form elements, whereupon it will be closed automatically. From the docs:
<% form_tag '/posts' do -%>
<div><%= submit_tag 'Save' %></div>
<% end -%>
# => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
Short version (see also: http://dev.rubyonrails.org/ticket/7391):
</form>
?
Correct version:
<% form_tag '/someform' do -%>
<div><%= submit_tag 'Submit' %></div>
<% end -%>
My way to fix it is to include the submit button right before the end and make the button not display.
<%= submit_tag('',style: 'width:0;height:0;display:none;') %>
<% end %>
This puts the ending tag right where I need it.
My fix is placing the form tag just above the table tag.
<%= form_tag(update_password_path, :method=> "post") do |f| %>
<p id="notice"><%= notice %></p>
<table align="center">