I just did this last night, and here's what I used. Note that I send the whole widget back (and use JSONP to add it to the list on my page):
def create @widget = Widget.new @widget.update_attributes(widget_params) render json: @widget, callback: params[:callback] end Walter On May 25, 2015, at 3:40 PM, Taras Matsyk <li...@ruby-forum.com> wrote: > 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+unsubscr...@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. -- 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+unsubscr...@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/69416A14-6674-4997-A4DF-506FA8E7CB4E%40wdstudio.com. For more options, visit https://groups.google.com/d/optout.