Reputation: 37
So, I have a method "create" inside my controller, but once I create my model, instead of showing the model itself, I want my view to show just the URL of that model, eg: "http://www.myapp.com/model/1".
I defined another action inside my controller, which is called "url", and once the model is saved (inside create method), I am doing like this:
format.html { render :action => "url" }
So in this url view I want to show the full url of my model.
How can I get the model url to place there?
Upvotes: 0
Views: 153
Reputation: 1326
You can use the url_for
method that is available in every controller.
url_for(:action => 'login', :controller => 'members',
:only_path => false, :protocol => 'http')
would result in (example):
http://www.example.com/members/login/
Source: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for
And as answered before me (apparently the answer was deleted), you can call url_for
by giving it an object as well (such as a newly created model instance):
# Create a new instance of a model:
@new_order = Order.new(...)
# And then get the url for that specific instance's location:
url_for(@new_order)
Hope I helped!
Upvotes: 2
Reputation: 3365
You need to add the action to your config/routes.rb
get "model", :to => "model#url"
Or for a member action
get "model", :to => "model#url", :on => :member
Upvotes: 0