Я пишу простую форму для редактирования атрибутов клиента (с Rails 3.1 и Ruby 1.9.3).Код контроллера:
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
В представлении:
<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>
Сообщение об ошибке, полученное при открытии страницы редактирования (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 %>
Первое, чтопроверьте, передается ли @cust [0] как объект nil. В консоли rails я проверил (с предоставленными cust_id и 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">]
Также из регистратора, который я вставил в контроллер, на самом деле есть действительный иправильный вывод cust_id в журнале. Итак, почему я по-прежнему получаю nil - это не сообщение об ошибке символа? Я также проверил модель CustOutput и присутствуют все поля attr_accessible.