RoR - Closing form_tag - ruby-on-rails

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

Related

Extra parameter in link_to method

The following is a piece of code for index action. Can somebody explain me the use of a third parameter, :class => 'action show' in the link_to helper in line numbers 27,28 and 29. The code seems to work fine without this as well. I'm a rails rookie and thanks in advance.
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", '#', :class => 'action new') %>
<table class="listing" summary="Subject list">
<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"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center">
<%if(subject.pages!=nil)%>
<%= subject.pages.size %>
<% else %>
<%= "1" %>
<% end %>
</td>
<td class="actions">
<%= link_to("Show", {:action => "show",:id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", '#', :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
It adds the class attribute to the html-tag.
So
<%= link_to("Edit", '#', :class => 'action edit') %>
will be rendered as:
Edit
You can put any html options as third argument to the link_to helper.
http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to

passing html input from view to controller in rails

My repo: https://github.com/Gtar69/artstore_hw2 => It's my shopping cart project
Now I want to input a value in the carts/index.html to change cart_item quantity.
No idea how to do that in rails
In typical php it's like
<form action="backend.php">
<td><input type="number" name= "kobe">
<input type="submit" value="submit/>
</td>
However, I want simplify the coding structure instead of using form.
My idea is => view/carts/index.html
<tbody>
<% current_cart.items.each do |product| %>
<tr>
<td><%= render_product_photo(product.default_photo) %></td>
<td>
<%= link_to(product.title, admin_product_path(product)) %>
</td>
<td><%= product.price %></td>
<td>
<input type="number" name= "kobe" value = <%= CartItem.where(product_id: product.id).take!.count%>>
<input type="submit" value="shala">
</td>
<td><%= link_to("改變數量", change_item_quantity_carts_path(:product_id => product.id, :count => 2),
:method => :post , :class => "btn btn-primary btn-lg btn-danger") %></td>
<td><%= link_to("刪除物品", delete_item_carts_path(:product_id => product.id) , :method => :post, :class => "btn btn-primary btn-lg btn-danger") %></td>
</tr>
<% end %>
</tbody>
passing "kobe" variable to carts_controller
def change_item_quantity
product = Product.find(params[:product_id])
# :count => 2
current_cart.change_cart_item_quantity(product ,params[:kobe])
redirect_to carts_path
end
and do backend calculation in model/cart.rb
def change_cart_item_quantity(product, count)
#改變cart_item中的數量
c= CartItem.where(product_id: product.id).take!
c.count = count
c.save
end
I'm still struggling in how to pass "kobe" to controller. Any ideas?
Thanks in advance.
refer to guides.rubyonrails.org/form_helpers.html
I used
<td>
<%= form_tag("/carts/change_item_quantity", method: "post") do%>
<%= label_tag(:q, "改變數量") %>
<%= number_field_tag(:kobe, CartItem.where(product_id: product.id).take!.count)%>
<%= hidden_field_tag(:product_id, product.id)%>
<%= submit_tag("Change") %>
<% end %>
</td>
In that way, I can pass any parameter I needed to controller.

getting image name or path from file field control into controller's page

I have a file field control on my page like below:
<h2><%= #blah %>hello</h2>
<%= form_for(:ImageUpload, :html => { :id => "imageupload" }) do |f| %>
<table width="100%">
<tr>
<td align="left">
<%= file_field 'upload', 'datafile' %>
</td>
</tr>
</table>
<table width="92%">
<tr>
<td align="center">
<div class="button" style="margin-right:60px;">
<%= f.submit "Next", { :class => "buttonSearch"} %>
</div>
</td>
</tr>
</table>
<% end %>
And I want to use file field for getting image and set into user profile, for that I want to get image name or image path and i am using below:
def create
#blah = params[:upload]
upload_img = params[:upload]
render 'new'
end
But in the #blah variable, it gets me like this {"datafile"=>"index.jpg"}hello, How do I am getting image name or path from file field control into a controller's page, Kindly suggest me, waiting for your reply.
Thanks.
You might be missing something here
#blah = params[:upload]["datafile"]
Use gem carrierwave
Otherwise u need to do file operations to save the image get the images. Make your work simple.
See documentation here http://carrierwave.rubyforge.org/rdoc/
Please add in your form_for tag multipart: true option.
Syntax:-
<h2><%= #blah %>hello</h2>
<%= form_for(:ImageUpload, :html => { :id => "imageupload",:mulitipart => true}) do |f| %>
<table width="100%">
<tr>
<td align="left">
<%= file_field 'upload', 'datafile' %>
</td>
</tr>
</table>
<table width="92%">
<tr>
<td align="center">
<div class="button" style="margin-right:60px;">
<%= f.submit "Next", { :class => "buttonSearch"} %>
</div>
</td>
</tr>
</table>
<% end %>

How can I edit a single field for an array of object?

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

Rails 3 Form fields not submitting params

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>

Resources