开发者

Ruby On Rails REST: Customize URL pattern for POST request

开发者 https://www.devze.com 2023-03-22 01:11 出处:网络
I am new to Ruby On Rails and need some help impleme开发者_如何学运维nting REST protocol. Whenever you do a POST on REST you get a URL back e.g. http://my-site.com/id/1

I am new to Ruby On Rails and need some help impleme开发者_如何学运维nting REST protocol.

Whenever you do a POST on REST you get a URL back e.g. http://my-site.com/id/1 I need a customized response in URL format which I have given in example above. Lets say I am doing a post on parameter <main-id>123</main-id> The customized response I am looking for is http://my-site.com/123/id/1 What I want to implement is, whatever parameter ID I passed during a post I want that as a part of the response URL output.

Thanks for any help in advance.


You can specify any URL in your controller, e.g.:

def create
  ... # create record here
  redirect_to "/#{params[:main_id]}/id/#{@record.id}"
end

Of course, you'll probably want to use the url helper based on your defined route:

def create
  ... # create record here
  redirect_to my_oddball_path(@record, :main_id => 123)
end


Provided you are using Rails 3, you could just add this route with the proper controller/action

 match ':main_id/id/:id', :controller => 'foo', :action => "bar", :via => :post, :as => main_id

Then you can just with the helper main_id_path(main_id, @record)

0

精彩评论

暂无评论...
验证码 换一张
取 消