Rails submit button not working in Internet Explorer - ruby-on-rails-3

Bizzarre issue I've been working that is beyond frustrating. The submit button on one of my edit views is not working in Internet Explorer. It is a pretty standard layout using input type = submit (not a customized button_to or something).
The button works fine in Safari, Chrome, and Firefox on both Mac and Windows.
Edit submit buttons work fine on other pages in IE
Using nested_form_for, with :html => {:multipart => true}
Submit button
<%= f.submit("update", :class=>"post_button")%>
Has anyone had issues with submit buttons not working in IE?
== update ==
Updated jQuery to make sure it wasn't a nested_form issue. I actually think the link may be not been working before I added the nested form. However, it has always been a multipart form.
View
<head>
<%= stylesheet_link_tag 'profile' %>
<%= javascript_include_tag "jquery.min", "nested_form" %>
</head>
<%= nested_form_for(#profile, :html => {:multipart => true}) do |f| %>
<div class="content">
<div class="editprofilesub">
<div class="profile_title"> basic </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :firstname, "First Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :firstname, :class => "profilefield", :class=>"profilefield" %>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :lastname, "Last Name" %>
</td>
<td class="profileformleft">
<%= f.text_field :lastname, :class => "profilefield" %>
</td>
</tr>
<td class="profileformright">
<%= f.label :avatar, "User Image" %><br>
</td>
<td class="profileformleft">
<i>Please rotate images prior to uploading.</i> <br>
<%= f.file_field :avatar%>
</td>
</table>
<div class="profile_title"> location </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :city %>
</td>
<td class="profileformleft">
<%= f.text_field :city, :class => "profilefield" %>
</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="profileformright">
<%= f.label :state, "State" %>
</td>
<td class="profileformleft">
<%= f.select :state, Carmen.state_names(),{}, :class=> "state" %>
</tr>
</table>
<div class="profile_title"> about </div>
<table>
<tr>
<td class="profileformright">
<%= f.label :bio, "about" %>
</td>
<td class="profileformleft">
<%= f.text_area :bio, :rows => '5', :cols => '60', :class => "profilefield" %><br>
</td>
</tr>
<tr>
<td class="profileformright">
<%= f.label :dob, "Date of Birth" %>
</td>
<td class="profileformleft">
<%= f.date_select :dob,
{:start_year => Time.now.year,
:end_year => 1900,
:order => [:month, :day, :year]}%>
</td>
</tr>
</table>
</div>
<div class="right">
<%= f.submit("update", :class=>"post_button")%>
</div>
</div>
<%end%>

I just found a similar issue in my non-MVC application, in IE8 and below. When enter is used, the server-side click handler for the submit button wasn't being executed. The problem was that IE doesn't POST the submit button when enter is used (but does when it is clicked). So, .NET knows it's a post back, but not what control caused it.

Related

Logged out users are seeing error message "undefined method `leads' for nil:NilClass"

I made a simple app that lets users track their sales leads. Everything works except for the home page for logged out users, who get the error message - undefined method `leads' for nil:NilClass.
The index in the leads controller:
def index
#leads = current_user.leads.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end
The homepage:
<% if user_signed_in? %>
<div class="row">
<div class="span3">
<h2>Leads</h2>
</div>
<div class="pull-right">
<%= link_to 'Add New Lead', new_lead_path, class: 'btn btn-primary' %>
</div>
</div>
<div>
<table id="leads" class="display">
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Phone</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Website</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Email</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Notes</th>
<th class="hidden-tablet hidden-phone hidden-desktop-small">Last Updated</th>
</tr>
</thead>
<tbody>
<% #leads.each do |lead| %>
<tr>
<td><%= link_to lead.company_name, lead %></td>
<td><%= lead.contact %></td>
<td><%= lead.phone %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.website %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.email %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.notes %></td>
<td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.updated_at.strftime("%d %b %y") %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="hero-unit">
<h1>Welcome to SnapLeads</h1>
<p>
The simplest CRM ever.
</p>
<p>
<%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
</p>
</div>
<% end %>
Why would the page be trying to access the leads method if the user is logged out?
Because the code in your index action is always executed. You could use something like the following in your controller:
def index
#leads = current_user ? current_user.leads.all : nil
respond_to do |format|
format.html # index.html.erb
format.json { render json: #leads }
end
end

Rails 3 - How to Dynamically Duplicate Record and Submit Multiple Records on Single Form

I have an INVOICE form. To eliminate tons of typing, users need to enter invoice attributes, then click a button to copy the first record, change a couple attributes, then save all the records at once. Excel style. Help?
<% form_for #invoice, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table class="ExcelTable2007">
<tr>
<th class="heading">PO #<font color="red"> *</font></th>
<th class="heading">Invoice #</th>
<th class="heading">"Bill To" is Correct</th>
<th class="heading">Invoice Date</th>
<th class="heading">Date Received</th>
<th class="heading">AP Clerk Note</th>
<th class="heading">Division</th>
<th class="heading">GR Approver</th>
<th class="heading">GR Alternate Approver</th>
<th class="heading">Invoice Attachment</th>
</tr>
<tr>
<td class="heading"><%= f.text_field :po_number, :size => 10 %></td>
<td class="heading"><%= f.text_field :invoice_number, :size => 10 %></td>
<td class="heading"><%= f.check_box :bill_to_address %></td>
<td class="heading"><%= f.calendar_date_select "invoice_date", :time => false, :size => 12 %></td>
<td class="heading"><%= f.calendar_date_select "date_received", :time => false, :size => 12 %></td>
<td class="heading"><%= f.text_field :clerk_note %></td>
<td class="heading"><%= f.collection_select :division_id, Division.active.order(:division), :id, :division, :include_blank => true %></td>
<td class="heading"><%= f.collection_select :approver_username, Aduser.order(:last_name, :first_name), :username, :fullname, :include_blank => true %></td>
<td class="heading"><%= f.collection_select :alternate_approver_username, Aduser.order(:last_name, :first_name), :username, :fullname, :include_blank => true %></td>
<td class="heading"><%= f.file_field :attachment %></td>
</tr>
</p>
</table>
    <%= link_to "Add Invoice", clone_invoice_url(#invoice) %>
<br>
<br>
<div class="actions">
<%= f.submit "Submit" %>
</div>
</p>
<% end %>
I tried the following, but receive an invalid authenticity token:
def clone_invoice
#invoice = Invoice.find(params[:id]).dup
#invoice.save
end
Can anyone help me? Thanks in advance!
Try this instead.
def clone_invoice
#invoice = Invoice.find(params[:id])
#new_invoice = #invoice.dup
end
If your invoice has an attachment then you would also want to copy that.
dupfile(#invoice.attachement.to_s, #new_invoice.attachment.to_s)
In your routes file, try
match 'invoices/:id/invoice_clone' => "invoices#clone_invoice", :as => "invoice_clone"
Then in your link_to, change the clone_invoice_url to invoice_clone_path

div_for within a table

I am trying to create a table that populates based on all of the records for a specific model.
Here is the index.html.erb file
<%= div_for(#departments, :class => "test") do |department| %>
<tr>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
</tr>
<% end %>
This seems to properly create the table rows but does not put them into divs as expected.
I would like to be able to create one table row for each department and then be able to use AJAX to add or remove them.
If you want each row to be wrapped in a div, you'll need to add the div class and id to the tr tag, IE: (tr id="myid" class="class") rather than wrapping the div around the tr.
You can use the rails content_tag_for to generate this:
<% #departments.each do |department| %>
<%= content_tag_for(:tr, department, :class => "test") do %>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
<% end %>
<% end %>

Convert from haml to erb

I try to convert the code from haml to erb but I'm getting stuck and don't know why.
Here is the original code I want to convert: https://github.com/gmarik/simple-backend-example/blob/master/app/views/backend/resource/_index.html.haml
And here is what I have right now. Can someone take a look at it and give me some hints. Thanks.
I doubt this line the most:
%tr[resource]{odd_or_even}
I think that it might be like:
<tr> <% #resource{odd_or_even} %>
RubyMine gave me an error at this line:
<%= paginate collection %>
<% content_for(:header) do %>
<h1><%= resource_class.model_name.human(count: 2) %></h1>
<ul class="tabs">
<li class="active"><%= link_to "Index", "#" %></li>
<li><%= link_to "New", new_resource_path %> </li>
</ul>
<table class='zebra-striped'>
<thead>
<tr>
<% attributes.each do |attr| %>
<th> <%= resource_class.human_attribute_name(attr) %></th>
<th> </th>
</tr>
</thead>
<tbody>
<% collection.each do |resource| %>
<tr> <% #resource{odd_or_even} %>
<% attributes.each do |attr| %>
<td> <%= resource.public_send(attr).to_s.truncate(20) %> </td>
<td class='row-actions'>
<%= link_to 'show', resource_path(resource) %>
|
<%= link_to 'edit', edit_resource_path(resource) %>
|
<%= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?" %>
</td>
<% end %>
</tbody>
</table>
<%= paginate collection %>
Looking at the documentation here : http://haml.info/docs/yardoc/file.REFERENCE.html#object_reference_
I'd say it's :
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= resource.class.name.underscore %>">
This is to translate %tr[resource].
Now, the {odd_or_even} will just convert the result hash of the helper odd_or_even to map them as attributes on the tr.
If we take a look at the definition of the method here : https://github.com/gmarik/simple-backend-example/blob/master/app/helpers/backend/application_helper.rb
We see it's just a call to cycle in order to set an extra class. Therefore we end up with:
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= "#{resource.class.name.underscore} #{cycle("odd", "even", name: "rows")}" %>">
Now, all of this won't fix the problem of paginate. Add the error message if you're still stuck on this.

Rails create and update model Cant resolve paths

I cant understand why edit and new links doesn't work. I have controller named CargoController and have model Car.
CargoController is:
def new_auto
end
def edit_auto
end
def index
#cars=Car.find_all_by_UserId(session[:user_id])
if #cars.nil?
end
end
Car model is:
class Car < ActiveRecord::Base
validates_presence_of :TransportTypeId
validates_presence_of :CarModelId
set_primary_key :CarId
has_one :TransportType
has_one :CarModel
belongs_to :User
has_many :PropertyOfCar, :dependent => :destroy
has_many :CarProperty, :through => :PropertyOfCar
end
The View CargoController index.erb is:
<%if !#cars.blank?%>
<table width="100%" border="0" cellpadding="0px" cellspacing=1px">
<% #cars.each do |car| %>
<tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<table border="0" width="100%" style="color:black">
<tr>
<td width="15%" align="left" valign="top" >Тип ТС:</td>
<td width="70%" align="left" valign="top"><%= TransportType.find_by_TransportTypeId(car.TransportTypeId).Name %></td>
<td align="right" valign="top" rowspan="2">
<img alt="" src="/images/Truck-icon.png"/>
</td>
</tr>
<tr>
<td width="15%" align="left" valign="top">Car model:</td>
<td width="70%" align="left" valign="top"><%= CarModel.find_by_CarModelId(car.CarModelId).ModelName %></td>
</tr>
<tr>
<td align="center" valign="top" colspan="3">
<label class="Chars"> Properties</label>
</td>
</tr>
<tr colspan="3">
<td>
<%= button_to 'Edit', :controller=>:cargo,:action=>:edit_auto,:id=>car.CarId %>
</td>
</tr>
</table>
</td>
</tr>
<% end %>
</table>
<% end %>
the view edit and new is :
<div class="center">
<%= render 'form' %>
</div>
The template _form is:
<%= form_for(#car) do |form| %>
<table >
<tr>
<td>
<label for="transport_type">Car Type:</label>
</td>
<td>
<%= form.select(:TransportTypeId, TransportType.all.collect {|p| [ p.Name, p.TransportTypeId ] }, { :include_blank => 'Select type'},{:style=>'width: 200px'}) %>
</td>
</tr>
<tr>
<td>
<label for="transport_type">Car model:</label>
</td>
<td>
<%= form.select(:CarModelId, CarModel.all.collect {|p| [ p.ModelName, p.CarModelId ] }, { :include_blank => 'Select model' },{:style=>'width: 200px'}) %>
</td>
</tr>
<tr>
<td colspan="2">
<%= button_to "add property",{:controller=>:cargo,:action=>:new_property},{:class =>"Button_style"} %>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<%= form.submit "Add car", :class => "submit" ,:class =>"Button_style"%>
</td>
</tr>
</table>
<% end %>
So then i try to add or update car rails ask a car controller or write
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: <%= form_for(#car) do |form| %>
2: <table >
3: <tr>
4: <td>.
What shall i do to make it work?
In a routes i have
get "cargo/index"
get "cargo/new_auto"
match ':controller(/:action(/:id))'
I think that you don't create instance variables for each actions.
def new_auto
#car = Car.new
end
def edit_auto
#car = Car.find(params[:id])
end
And you should write "resources :cars" routes.rb as follwing:
resources :cars
I think that you should be able to see the new_auto page.