Hi!
I have many links like <%= link_to 'Edit', edit_content_path(content) %> but since my Rails update and some other route changes the path is wrong.
It leads to domain/contents instead of the correct domain/subfolder/contents... what is wrong?
Thanks!
Have you nested any routes in your routes.rb file?
If you you will have to add the parent to the path.
edit_parent_content_path(#parent, #content_id)
Something like that. Running ->
$ rake routes
Will help you determine if it is a route problem. (Try to find your current path in the results.)
If you still have a problem, post your routes.rb file.
Here is my routes.rb:
ActionController::Routing::Routes.draw do |map|
map.root :controller => "store"
map.resources :users
map.resources :orders, :controller => 'order'
map.resources :contents
map.resources :products
map.resources :kundservice => "store/kundservice"
map.resources :foretaget => "store/foretaget"
# Install the default routes as the lowest priority.
map.connect 'subfolder/:controller/:action/:id'
map.connect 'subfolder/:controller/:action/:id.:format'
end
Because of the hostcompany where I have my site, I must have it in a subfolder under my domain, www.domain.com/subfolder/store.
Thanks!
Related
I was viewing an episode in RailsCasts and was looking at the source code for episode 145.
This was his code for the routes.rb file
ActionController::Routing::Routes.draw do |map|
map.resources :orders
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
map.resources :line_items
map.resources :carts
map.resources :products
map.resources :categories
map.root :products
end
I was immediately thrown off. That looked like an entirely different syntax. Then I realized that this source code was published in 2010. I'm wondering if it's now obsolete, because I copied and pasted that code into my Rails Application and it isn't working.
Usually, what I do is
resources 'orders'
root 'products'
I don't know how I would rewrite map.current_cart.
This is the error message I get
NameError at/orders/new
undefined local variable or method 'map' for #<ActionDispatch::Routing::Mapper::0x4d90868>
This line is highlighted
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
Yes it is obsolete now.
Instead of map.resources just use resources.
So you should write
resources :orders
In Rails 4+ it would look something like this:
My::Application.routes.draw do
get '/cart', to: 'carts#show', defaults: { id: 'current' }
end
That may be post if that's how the route is exercised.
The map argument is now gone. My::Application depends on the name of the application in question.
As always, have the routing documentation handy when doing things like this.
If you're working on an older application you will have to follow the older conventions.
How is it possible to use hyphen in resources urls?
For example: /my-model/ or /my-model/1.
If I define route as resources :"my-model" I get syntax error because rails generates method def hash_for_my-models_url(options = nil).
I have found the solution:
resources "my-models", :as => :my_models, :controller => :my_models
UPDATE:
As Timo Saloranta said in comment it works without :controller => :my_models in latest Rails 3 versions.
You can use the :as option to configure resourceful routes with hyphenated URLs:
map.resources :my_model, :as => "my-model"
results in
my_model_index GET /my-model(.:format) {:action=>"index",
:controller=>"my_model"}
...etc...
Have you tried a custom route?
map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read'
This would invoke the 'read' method of 'my-model-controller.rb'.
I want the url of a user post to be like this:
example.com/users/username/post-name
How should I set this up?
Currently, the post model contains:
def to_param
name.parameterize
end
(to hyphenate the post name accordingly)
routes.rb contains:
map.resources :users
map.resources :posts
Preferably, I would like to say post_path(post) and this would generate the appropriate path for me by finding the post.user automatically.
What do I need to do to make this happen?
Also added
map.resources :users do |user|
user.resources :posts
end
One way more:
map.resources :users do |user|
user.connect ':name/:action', :controller => 'posts', :defaults => {:action => 'show'}
end
Available routes:
example.com/users/username/post-name
example.com/users/username/post-name/edit (any action)
Hi To make your application recognize routes like
example.com/users/username/post-name
you should add to your routes.rb
map.connect 'users/:username/:post', :controller => "users", :action => "test"
Then you can access params[:username] and params[:post] inside your controllers test action You should define it after map.resources :users but before map ':controller/:action/:id' but you must write your own helper then
That to_param looks okay, but the builtin helpers don't link to nested resources like you want, you'd have to use user_post_path(user, post). Ofcourse, you could write your own post_path helper method which does work the way you want.
def post_path(post)
url_for [post.user, post]
end
I am working on a project in ruby on rails and I am having a very difficult time with a basic problem. I am trying to call a custom action in one of my controllers, but the request is somehow getting redirected to the default 'show' action and I cannot figure out why.
link in edit.html.erb:
<%= link_to 'Mass Text Entry', :action=>"create_or_add_food_item_from_text" %>
Error from development.log:
ActiveRecord::RecordNotFound (Couldn't find Menu with ID=create_or_add_food_item_from_text): app/controllers/menus_controller.rb:20:in `show'
routes.rb file:
ActionController::Routing::Routes.draw do |map|
map.resources :nutrition_objects
map.resources :preference_objects
map.resources :institutions
map.resources :locations
map.resources :menus
map.resources :food_items
map.resources :napkins
map.resources :users
map.resource :session, :controller => 'session'
map.root :controller=>'pages', :action=>'index'
map.about '/about', :controller=>'pages', :action=>'about'
map.contact '/contact', :controller=>'pages', :action=>'contact'
map.home '/home', :controller=>'pages', :action=>'index'
map.user_home '/user/home', :controller=>'rater', :action=>'index'
map.user_napkins '/user/napkins', :controller=>'rater', :action=>'view_napkins'
map.user_preferences '/user/preferences',:controller=>'rater', :action=>'preferences'
map.blog '/blog', :controller=>'pages', :action=>'blog'
map.signup '/signup', :controller=>'users', :action=>'new'
map.login '/login', :controller=>'session', :action=>'new'
map.logout '/logout', :controller=>'session', :action=>'destroy'
# Install the default routes as the lowest priority.
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Menus_controller.rb:
class MenusController < ApplicationController
...
def create_or_add_food_item_from_text
end
...
end
create_or_add_food_item_from_text.html.erb simply has a div to show a form with a text box in it. I have the rest of my app working fine, but this is stumping me.
Any help is appreciated.
Try adding the route to your file explicitly, before the :menus resources:
map.connect "/menus/create_or_add_food_item_from_text",
:controller => "menus", :action => "create_or_add_food_item_from_text"
map.resources ...
Routes declared earlier have higher priority, and the problem here is that map.resources actually prevents certain paths from being routed.
Even regardless of this issue, it's good practice to map all paths explicitly, either through resources or named/unnamed routes, and ultimately eliminate the generic :controller/:action and :controller/:action/:id routes from your app.
link_to expects the path to your action as the second parameter - it looks like you are passing link_to the wrong path value. Check the development log to see what path rails thinks you are looking for.
If I want to provide an alias for a controller, I can use map.resources :rants, :controller => 'blog_posts' yoursite.com/rants points to the blog_posts controller fine.
How do I give an alias to a nested resource, for example yoursite.com/users/5/rants ?
You may want to try:
map.resources :rants, :controller => 'blog_posts'
map.resources :users do |users|
users.resources :rants, :controller => 'blog_posts'
end
This will give you the yoursite.com/users/5/rants/ url that you are looking for and it will generate the handy methods (for example: users_rants_path(#user))
Hope this helps.