2010-01-03 4 views
0

has_many build method, Rails초보자 다시 올랐다. 오류는 없지만 데이터가 저장되지 않는다.

내 마지막 질문이었습니다. 이제 작동합니다 : 오류 없음. 새로운 문제는 새로운 단위 전환이 원료 ID와 연결되지 않는다는 것입니다. 나는 이것이 단지 "build method"에서 "일하기로되어있는"것이라고 생각했다.

단위 변환 컨트롤러 :

def new 
    @ingredient = Ingredient.find(params[:ingredient_id])  
    @unit_conversion = @ingredient.unit_conversions.build 
end 

def create 
    @ingredient = Ingredient.find(params[:ingredient_id])  
    @unit_conversion = @ingredient.unit_conversions.build(params[:unit_conversion]) 

    if @unit_conversion.save 
    flash[:notice] = "Successfully created unit conversion." 
    redirect_to ingredient_unit_conversions_url(@ingredient) 
    else 
    render :action => 'new' 
    end 
end 

단위 변환 모델 :

class UnitConversion < ActiveRecord::Base 
    belongs_to :ingredient 
end 

성분 모델 : 도움을

class Ingredient < ActiveRecord::Base 
    belongs_to :unit 
    has_many :unit_conversions 
end 

감사합니다, 오늘은 거친 학습 곡선을 찾는거야 :)

편집 : 한 가지 더 중요한 것은 ..

<h1> New Derived Unit </h1> 
<% form_for([@ingredient, @unit_conversion]) do |f| %> 
     <% f.error_messages %> 

     <p> 
      <%= f.label :name %> 
      <%= f.text_field :name%> 
     </p> 
     <p> 
      <%= f.label :conversionToBase%> 
      <%= f.text_field :conversionToBase%> 
     </p> 
     <p> 
      <%= f.submit "Create" %> 
     </p> 
    <% end %> 
<% link_to 'Back', url_for(:controller => 'ingredients', :action => 'show', :id => @ingredient)%> 

답변

2

당신이 당신의 형태로 form_for [@ingredient, @unit_conversion]가 있는지 확인 new.html.erb.

new.html.erb

<% title "New Unit Conversion" %> 
<%= render :partial => 'form' %> 
<p><%= link_to "Back to List", ingredient_unit_conversions_path(@ingredient) %></p> 

_form.html.erb

<% form_for [@ingredient, @unit_conversion] do |f| %> 
    <%= f.error_messages %> 
    <p> 
    ... fields here 
    </p> 
    <p><%= f.submit "Submit" %></p> 
<% end %> 
+0

나는 아주 분명하지 않다 같아요. 양식에 입력 된 데이터는 테이블에 저장되지만 ingredients_id는 저장되지 않습니다. 나는 당신이 new.html.erb를 보여주기 위해 대답 한 후에 그 글을 편집했다. 파일에 이미있는 것 같아. – Joan

관련 문제