In the last we generated a useful link for our mail.
generate an real url with rails for an e-mail
This works fine, very fine. But we want to call the shoe-action :-)
<%=link_to('Link', :controller => 'mycontroller', :action => 'show', :id => #mycontroller.id )%>
The url looks so
http://localhost:3000/mycontroller/show/10
we need this structure
http://localhost:3000/mycontroller/10
The rake routes command said this
mycontroller GET /mycontroller/:id(.:format) mycontroller#show
How do we get the needed structure? Do we need to edit our routes, or is there another way to get the right url?
Try to use the route helper
mycontroller_url(#mycontroller.id)
instead the {:controller => ..., :action => ...}
your link_to helper should look like this for better example i use the user show action
rake routes return this
user GET /users/:id(.:format) users#show
and now I know I can use the user_url(#user) helper
<%=link_to('Link', user_url(#user) )%>
Related
I am having difficulties with routings in ruby on rails. I have same url format in different controllers and I am trying to send params to site controller but it goes to pages controller instead. Any help?
Route.rb
resources :sites, :path => '', :except => [:index] do
resources :pages, :path => '' do
end
end
When I run rails routes command I am getting something look like:
site_pages GET /:site_id(.:format) pages#index
site GET /:id(.:format) sites#show
View
<%= link_to 'GO', site_path(site) %>
This code should go to the site#show but it goes to pages#index and it gives me this error message below:
NoMethodError in PagesController#index
undefined method `site_id' for nil:NilClass
Parameters:
{"site_id"=>"site A"}
I have tried:
<%= link_to '', site_path(site), :controller => "sites", :action => "show"%>
<%= link_to '', site_path(id: site.id)%>
but it doesn't work. Any help will be very appreciate
In addition to #jvillian's comment, your routes aren't functional.
Imagine an user wants to see the site with id 1. She could type in the URL my_domain.com/1. Now, she wants to check that site's pages, so she types in my_domain.com/1. How can Rails know what is she referring to?
Since pages is nested inside of sites, you'll (usually) need to have a site to have pages. To accomplish this in your scenario, remove the :path => '' from the pages resource. This will give you routes that look like this:
site_pages GET /:site_id/pages(.:format) pages#index
site GET /:id(.:format) sites#show
Then, you'll need to have a site id anytime you want to access/create its pages. Keep in mind, though, that if you pass a route like mydomain.com/anything_here, you're going to get directed to the sites#show action.
I am new to rails. My rails version is 2.3.5. I found usage like:
In controller, a destroy method is defined and in view, you can use :action => "delete" to fire that method. Isn't the action name has to be the same as the method name? Why delete is mapped to destroy?
Again, in my controller, I define a method called destroy to delete a record. In a view, I have <%= link_to "remove", :action => 'destroy', :id => myrecord %>. But it never works in practice. Every time I press the remove link, it redirects me to the show view, showing the record's content. I am pretty sure that my destroy method is:
def destroy
#myobject = MyObject.find(params[:id])
#myobject.destroy
#redirect_to :action = 'index'
end
If I change the method name from destroy to something like remove_me and change the action name to remove_me in the view, everything works as expected.
In the above two weird problems, I am sure there is no tricky routing set in my configuration.
All in all, seems the destroy and delete are mysterious keywords in rails. Can anyone explain this to me?
You probably set MyObject as a resource in routes.rb. Resources get a couple of routes that don't directly match the name of the action. When you use an action name that does not match the routes defined by the resource, you'll get the default route which directly maps to the name of the action.
I found that this link explains rails' routing very well. Take a look at the "RESTful routing" section.
If you are using REST routing, destory only support delete method. you can change your code like this
link_to "remove", :action => 'destroy', :id => myrecord", :method => :delete
Adding :method => :delete
rails will add a hidden input with name "_method", value "delete"
Replace all :post => true with :method => :post
Let's say I have a Ruby on Rails blogging application with a Post model. By default you would be able to read posts by http://.../post/id. I've added a route
map.connect ':title', :controller => 'posts', :action => 'show'
that will accept http://.../title (titles are unique) and the controller will do a query for the title and display the page. However when I am now calling <%= link_to h(post.title), post %> in a view, Rails still gives me links of the type post/id.
Is it possible to get Rails to automatically create the pretty links for me in this case?
If you are willing to accept: http:/.../1234-title-text you can just do:
def to_param
[id, title.parameterize].join("-")
end
AR::Base.find ignores the bit after the id, so it "just works".
To make the /title go away, try naming your route:
map.post ':id', :controller => 'posts', :action => 'show', :conditions => {:id => /[0-9]+-.*/ }
Ensure this route appears after any map.resources :posts call.
You can override ActiveRecord's to_param method and make it return the title. By doing so, you don't need to make its own route for it. Just remember to URL encode it.
What might be a better solution is to take a look at what The Ruby Toolbox has to offer when it comes to permalinks. I think using one of these will be better than to fixing it yourself via to_param.
I would use a permalink database column, a route, and I normally skip using link_to in favor of faster html anchor tags.
Setting your route like:
map.connect '/post/:permalink', :controller => 'post', :action => 'show'
then in posts_controller's show:
link = params[:permalink]
#post = Post.find_by_permalink(link)
You link would then be
Link
then in your create method, before save, for generating the permalink
#post = Post.new(params[:post])
#post.permalink = #post.subject.parameterize
if #post.save
#ect
There is a Gem for you to get this done perfectly
https://github.com/rsl/stringex
Is there a way to look up the HTML for a given controller action? For example, I would like to be able to associate GET with index and PUT with update. I want to be able to do this dynamically based on the routes.
I can get the action methods for each controller using Controller.action_methods, but this returns a set of strings of action methods. Ideally what I would like is a hash of the form: {:action => :verb}.
Read the rake routes task, that will provide insight:
e.g:
users GET /users(.:format) {:controller=>"users", :action=>"index"}
I assume this is what you are after?
:method is part of a :conditions hash you can pass in to map.connect
map.connect 'post/:id', :controller => 'posts', :action => 'create_comment',
:conditions => { :method => :get }
To provide a useful object with all controllers, actions, and associated verbs
def all_routes
#all_routes ||= Rails.application.routes.routes.map do |route|
{ alias: route.name,
path: route.path.spec.to_s,
controller: route.defaults[:controller],
action: route.defaults[:action],
verb: route.verb.source.to_s.delete('$'+'^')
}
end
end
I am developing a rails app and have a question.
In my routes.rb:
map.connect 'admin', :controller => "/admin/users", :action => "index"
So when I go to "http://mydomain.com/admin", it redirects to "http://mydomain.com/admin/users/index".
However, the address remains as "http://mydomain.com/admin".
Thus, links in the page are wrong because they are created based on "http://mydomain.com/admin".
What's the solution to this problem?
Sam
try this:
map.connect 'admin/:action/:id', :controller => 'admin/users'
Your code is not redirecting the browser it's just setting up /admin and /admin/users to trigger the same action.
You could try:
map.connect 'admin', :controller => "/admin/users", :action => "redirect_to_index"
Then in your controller write:
def redirect_to_index
redirect_to :action => :index
end
This will send a redirect to the browser, causing it to display the correct URL.
Hopefully there is a better method that only involves routes.rb. This site might be helpful -> redirect-routing-plugin-for-rails
Make sure any same-domain links on the page start with a /, and use the full path. Generally you should use Rails route methods to generate your links when possible. Same goes for using the image_tag and stylesheet_link_tag helpers.
So if you have a link to "privacy.html", change it to "/privacy.html" and you should be all good no matter where in the route structure you are. This is extra nice when you start extracting your view code out to re-usable partials.
Use link_to and button_to (in UrlHelper)