I'm writing a simple form for editing customer's attributes (with Rails 3.1 and Ruby 1.9.3). The controller code:
def edit
cust_id = params[:cust_id]
output_id = params[:output_id]
@cust = CustOutput.find(:all, :conditions => {:cust_id => cust_id, :output_id => output_id })
logger.info(@cust[0].cust_id)
end
In the view:
<h2> Editing customer <%=@cust[0].cust_id %> with output id <%=@cust[0].output_id %></h2>
<div>
<%= form_for @cust[0] do |cust| %>
<%= render "shared/error_messages", :target => @cust[0] %>
<div id='id' class='outerDiv'>
<%= cust.text_field :cust_id, :size => 20 %>
</div>
<div id='email1' class='outerDiv'>
<label for='email1'>Email Part1</label>
<%= cust.text_field :email_part1, :size => 20 %>
</div>
<div id='email2' class='outerDiv'>
<label for='email2'>Email Part 2</label>
<%= cust.text_field :email_part2, :size => 20 %>
</div>
<div id='dt' class='outerDiv'>
<label for='dt'>Delivery Type</label>
<%= cust.text_field :delivery_type, :size => 20 %>
</div>
<%= cust.submit "Save" %>
<span class="cancel"><%= link_to "Cancel", :action=>"load", :cust_name=>"#{@cust[0].cust_id}" %>
<% end %>
</div>
The error message I got from opening edit page (http://localhost:3000/edit?cust_id=2&output_id=6):
nil is not a symbol
Extracted source (around line #3):
1: <h2> Editing customer <%=@cust[0].cust_id %> with output id <%=@cust[0].output_id %></h2>
2: <div>
3: <%= form_for @cust[0] do |cust| %>
4: <%= render "shared/error_messages", :target => @cust[0] %>
5: <div id='id' class='outerDiv'>
6: <%= cust.text_field :cust_id, :size => 20 %>
The first thing is to check whether @cust[0] passed in is a nil object. In rails console I checked (with the provided cust_id and output_id)
@cust = CustOutput.find(:all, :conditions => {:cust_id => 2, :output_id => 6 })
CustOutput Load (0.7ms) SELECT "cust_outputs".* FROM "cust_outputs" WHERE "cust_outputs"."cust_id" = 22 AND "cust_outputs"."output_id" = 6
=> [#<custOutput cust_id: 2, output_id: 6, email_part1: "abc@company.com", email_part2: nil, delivery_type: "ftp">]
Also from the logger I put in controller, there's actually a valid and correct cust_id output in the log. So, why do I still get nil is not a symbol error message? I've checked on CustOutput model as well and all attr_accessible fields are present.
It's a old question, but I recently have the same problem when doing maintenance in a old project. This project uses a different column for ID, but the primary key was not specified in the model.
After added these line all works:
self.primary_key = 'uuid'
Project use Rails 3.1 and MySQL 5.
如果您只想为集合中的第一个客户呈现表单,那么使用@cust.first
怎么@cust.first
?
Try to use: ( :first
instead of :all
)
@cust = CustOutput.find(:first, :conditions => {:cust_id => cust_id, :output_id => output_id })
And in your form_for
remove all the [0]
references
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.