Ruby on Rails Monday, May 25, 2015

Hi everyone,

A client sends a post query which contains all required data to create a
Todo except 'id'. From what I know 'id' field will be created when
@todo.save is called. So the question is how to inject id (if it is
possible and the right way) in to Controlller.create action?

Here is what I am trying to do:
def create
logger.info "todo#create -> #{params[:title]}"
@todo = Todo.new(title:params[:title])
@todo.save
logger.info "todo#create ID: -> #{@todo.id}"
render @todo.id.to_json(), status: :ok
end

And I get different 500 error unless I return just a status "head :ok"
Is it a right way to use rails API? And if yes is it possible to get
back an id of created object?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9011bdcbcef1a0cebc0fd674d1178131%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment