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.
Related
<% include partials/header.ejs %>
<div class="table-wrapper">
<% if (players.length > 0) {%>
<table class="table table-hovered">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Image</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Position</th>
<th scope="col">Number</th>
<th scope="col">Username</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<% players.forEach((player, index) => { %>
<tr>
<th scope="row"><%= player.id %></th>
<td><img src="/assets/img/<%= player.image %>" class="rounded-circle player-img" alt=""></td>
<td><%= player.first_name %></td>
<td><%= player.last_name %></td>
<td><%= player.position %></td>
<td><%= player.number %></td>
<td>#<%= player.user_name %></td>
<td>
Edit
Delete
</td>
</tr>
<% }) %>
</tbody>
</table>
<% } else { %>
<p class="text-center">No players found. Go <a href="/add" >here</a> to add players.</p>
<% } %>
</div>
</div>
</body>
</html>
This is the error being displayed after running the .ejs file from terminal,
SyntaxError: Unexpected identifier in C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\views\index.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
at new Function ()
at Template.compile (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:626:12)
at Object.compile (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:366:16)
at handleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\ejs\lib\ejs.js:459:10)
at View.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\A\Desktop\Ms\demo-app\node-mysql-crud-app\node_modules\express\lib\response.js:1012:7)
I am new to expressjs and this a code from a demo app in GitHub.
It would be a great help if this problem is solved.
Read this document https://github.com/tj/ejs and i think your foreach loop having some problem check this first and then try once.
<% for(var i=0; i<players.length;i++) { %>
<tr id="idrow_<%= players[i]._id%>">
<td id="id">
<%= players[i]._id%>
</td>
<td id="name">
<%= players[i].first_name%>
</td>
<td id="phone">
<%= players[i].last_name%>
</td>
<td id="email">
<%= players[i].position%>
</td>
<td id="vehicle">
<%= players[i].number%>
</td>
<td id="other">
<%= players[i].user_name%>
</td>
<td><button type="button" class="btn btn-secondary" id="edtbtn" onclick="edituser('<%= players[i]._id %>')" name="edit"><b>edit</b></button></td>
<td><button type="button" class="btn btn-secondary" id="delbtn" onclick="deleteuser('<%= players[i]._id %>')" name="delete">
<span><strong>Delete</strong></span>
</button>
</td>
</tr>
<% } %>
========================================================================
Here's the Bootply
How can i render these panels tp be inline? I don't see what I'm missing here, i have a container, I have a row, I have the col-md-4 nested within the row?
For context, each of those panels is written in a '_quotes.html.erb' partial, which is rendered by a Quotes.all.each block within the 'index.html.erb' view.
index.html.erb
<div class="container">
<div class="row">
<% #quotes.each do |quote| %>
<%= render :partial => "quote", locals: {quote: quote} %>
<% end %>
</div>
_quote.html.erb
<% #quote = local_assigns[:quote] %>
<%= link_to quote_path(#quote) do %>
<section id="quotes">
<!-- <div class="container"> -->
<div class="col-md-4 col-md-4 col-md-4 text-center">
<div class="panel panel-success panel-quote link-panel">
<div class="panel-heading">
<strong>GLA</strong>
</div>
<div class="panel-body">
<p><strong>Quote ID; <%= #quote.id %></strong></p>
</div>
<table class="table table-bordered">
<tr>
<td>Company name</td>
<td><%= #quote.co_name %></td>
</tr>
<tr>
<td>Company number</td>
<td><%= #quote.co_number %></td>
</tr>
<tr>
<td>Office postcode</td>
<td><%= #quote.postcode %></td>
</tr>
<tr>
<td>Industry</td>
<td><%= #quote.industry %></td>
</tr>
<tr>
<td>Previous cover</td>
<td><%= #quote.prev_cover %></td>
</tr>
<tr>
<td>Lives overseas</td>
<td><%= #quote.lives_overseas %></td>
</tr>
<tr>
<td>Scheme start date</td>
<td><%= #quote.scheme_start_date %></td>
</tr>
<tr>
<td>Payment frequency</td>
<td><%= #quote.payment_frequency %></td>
</tr>
<tr>
<td>Commission level</td>
<td><%= #quote.commission_level %></td>
</tr>
</table>
</div>
</div>
<!-- </div> -->
</section>
</div>
<% end %>
There's a couple of mistakes
You have only a single col-md-4 which means all children will fit to that single column.
You have another container div inside your col-md-4 which basicly resets the whole thing and again a single col-md-4 in there.
Edited Bootply
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
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
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.