activerecord - What is the Correct Ruby on Rails Syntax to Write to a Nested Model within a Virtual Attribute? -
trying divide , conquer these problems (1, 2) still having. write first step of blt recipe in nested, many-to-many model virtual attribute. later on have more complex form, doing in model.
i have hard-coded in model except name of recipe. here recipe model:
class recipe < activerecord::base has_many :steps, :class_name => 'step' has_many :stepingreds, :through => :steps has_many :ingredients, :through => :stepingreds accepts_nested_attributes_for :steps, :stepingreds, :ingredients attr_writer :name_string after_save :assign_name def name_string self[:name] end def assign_name if @name_string self[:name] = @name_string self[:description] = "addictive sandwich" self.steps = step.create({ :number => 1, :instructions => 'cook bacon', :stepingreds => [{ :ingredient => { :name => 'bacon' }, :amount => 4 } ] }) end end
and here form
<%= form_for @recipe |f| %> <%= f.error_messages %> <p> <%= f.label :name_string, "name" %><br/> <%= f.text_field :name_string %> </p> <p><%= f.submit %></p> <% end %>
i "nameerror in recipescontroller#create, undefined local variable or method `attribute' #". think have more 1 error seems should work me. doing wrong?
thanks!
edit - here recipecontroller create action
def create @recipe = recipe.new(params[:recipe]) if @recipe.save redirect_to @recipe, :notice => "delicious blt created!" else render :action => 'new' end end
here's simple rails application need:
Comments
Post a Comment