2012-03-05 2 views
2

저는 Rails 3.1 및 Ruby 1.9.3을 사용하여 고객의 속성을 편집하기위한 간단한 양식을 작성하고 있습니다. 컨트롤러 코드 :nil은 form_for의 상징이 아닙니다

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 <%[email protected][0].cust_id %> with output id <%[email protected][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/편집하는 cust_id = 2 & output_id = 6?) :

nil is not a symbol 
Extracted source (around line #3): 

1: <h2> Editing customer <%[email protected][0].cust_id %> with output id <%[email protected][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 객체인지 확인하는 것이 가장 좋습니다. 레일 콘솔 I는

@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: "[email protected]", email_part2: nil, delivery_type: "ftp">] 

는 또한 I 제어기 넣어 로거에서 실제로 로그에서 유효하고 정확한 출력 CUST_ID있다 (제공된 CUST_ID 및 output_id으로) 체크. 그래서, 왜 아직도 nil은 심볼 에러 메시지가 아닌가? 나는 CustOutput 모델을 점검했고 모든 attr_accessible 필드가 있는지 확인했다.

답변

0

컬렉션의 첫 번째 고객에 대한 양식을 렌더링하려는 경우 @cust.first을 사용하는 것이 어떻습니까? 모든 [0] 참조

0

한번에 사용하기 오래된 프로젝트. 이 프로젝트는 ID에 다른 열을 사용하지만 기본 키는 모델에 지정되지 않았습니다. 이 라인 모든 작품에 추가 한 후

는 :

self.primary_key = 'uuid' 

프로젝트 사용 3.1 레일와 MySQL 5

+0

네를, 나는 그것을 시도했다. 결과를 전혀 변경하지 않습니다. – swingfuture

0

그것은 오래된 질문을 제거

@cust = CustOutput.find(:first, :conditions => {:cust_id => cust_id, :output_id => output_id }) 

(:first 대신 :all의) 그리고 당신의 form_for에 만에 유지 보수를 할 때 나는 최근에 같은 문제가 있습니다 :

관련 문제