Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My recipes model is as follows

has_many :quantities
has_many :ingredients, :through => :quantities, :uniq => true
has_many :sizes, :through => :quantities, :uniq => true

And the controller action im trying to refactor

class Admin::QuantitiesController < Admin::AdminController
  layout 'admin'

  authorize_resource :class => :controller

  def create
    recipe        = Recipe.find(params[:recipe_id])
    values      = params[:quantity][:ingredient]
    @quantities = Quantity.find(values.keys)

    @quantities_errors = {}
    @quantities.each do |quantity|
        if quantity.value != values[]
            quantity.value = values[]
            if quantity.valid?
                @quantities_errors[] = quantity.errors.messages

    unless @quantities_errors
        redirect_to edit_admin_recipe_path(recipe), notice: 'Ingredients was successfully added.'
        redirect_to edit_admin_recipe_path(recipe), alert: @quantities_errors


share|improve this question
This is confusing. You are editing a recipe, right? (edit_admin_recipe_path), then why are you getting the POST in QuantitiesController#create? It should be in RecipesController#update and use Rails infrastructure (mainly accepts_nested_attributes_for), to remove all this boilerplate. – tokland Jan 26 '13 at 13:39
Thanks ever so much, after your comment I found and managed to get it posting to the recipe model and using accepts_nested_attributes_for. I knew the code was dodgy hence why I posted. I didn't know about accepts_nested_attributes_for before now. – Karl Entwistle Jan 27 '13 at 18:52
up vote 1 down vote accepted

Some notes:

  • This is a edit_admin_recipe form, so you should POST to Admin::RecipesController#update.

  • Use Rails infrastructure for the models (accepts_nested_attributes_for) to save nested data. Controllers must be kept simple and clean.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.