Rails: creating a model in the new action

Posted by Joseph Silvashy on Stack Overflow See other posts from Stack Overflow or by Joseph Silvashy
Published on 2010-05-08T18:46:23Z Indexed on 2010/05/08 18:58 UTC
Read the original article Hit count: 196

Filed under:

I have an interesting situation, well it's probably not that unique at all, but I'm not totally sure how to tackle it. I have a model, in this case a recipe and the user navigates to the new path /recipes/new however the situation is that I need to be able to have the user upload images and make associations to that model in the new action, but the model doesn't have an ID yet.

So I assume I need to rethink my controller, but I don't want to have redirects and whatnot, how can accomplish this?

Here is the basic controller, barebones obviously:

...

def new
  # I should be creating the model first, so it has an ID
  @recipe = Recipe.new
end

def create
  @recipe = Recipe.new(params[:recipe])
  if @recipe.save
    redirect_to @recipe
  else
    render 'new'
  end
end

...

update

Perhaps I can have a column thats like state which could have values like new/incomplete/complete or what-have-you. I'm mostly trying to figure out what would also be most efficient for the DB.

It would be nice if I could still have a url that said '/new', instead of it be the edit path with the id, for usability sake, but I'm not sure this can be simply accomplished in the new action of my controller.

Thoughts?

© Stack Overflow or respective owner

Related posts about ruby-on-rails