So I have a route defined like this for my "evisit" controller and "all_messages" action
match "evisits/:token/all_messages" => "evisits#all_messages", :as => :all_evisit_messages
Shows up in rake routes like this:
all_evisit_messages /evisits/:token/all_messages(.:format) {:controller=>"evisits", :action=>"all_messages"}
And I can manually go to it just fine however if I try to redirect to it like so:
redirect_to all_evisit_messages_url(#evisit.token)
I get a "No routes match - Routing Error" as if the route doesn't exist. I think I defined it correctly... anything I'm missing?
Try doing this instead:
redirect_to all_evisit_messages_url(:token => #evisit.token)
Does that work?
Related
In my first approach with Rails I have simply create a void SayController and static hello.rhtml view but when the page http://localhost:3000/say/hello started return me a Routing Error like this:
No route matches [GET] "/say/hello"
Try running rake routes for more information on available routes.
Rails version: 3.2.6
Seems like you didn't add a route for hello to your config/routes.rb file.
YourApp::Application.routes.draw do
match 'say/hello' => 'say#hello', :as => :hello
end
This will match route say/hello to controller say (the part before #) and action hello (the part after #).
:as => :hello makes it a named route so you can refer to it as hello_path from within your app.
The error message tells you to run rake routes (from the console) which will show you the existing routes in your app.
You should have something in your config/routes.rb to define that route. Try:
match 'say/hello' => 'say#hello', :as => 'say_hello'
The go to localhost:3000/say/hello
Also check out this documentation:
http://guides.rubyonrails.org/routing.html
I assume, controller: say and action: hello
Add following to config/route.rb
get 'say/hello' => 'Say#hello'
I have the following in my routes.rb:
resources :users do
resources :decisions
end
/users/new works fine, but /users/:id/decisions/new gives me:
No route matches {:controller=>"decisions", :format=>nil}
<%= link_to "New decision," new_user_decision_path(#user) %> gives me the same error.
I've looked in my rake routes and the action and the helper are listed. All of the actions listed have a (.:format) suffix option, but I don't understand how all of my DecisionsController actions are working fine without a format option except 'new'.
How is :format set to nil and what is its default?
it looks like you used a singular decision in your url when it should be decisions. Your url should be /users/1/decisions/new.
However, you should be using a named route to stop this from happening. You should see something like new_user_decision via rake routes. You can then use the new_user_decision_path for all of your links.
I would like to do something to this effect, I believe:
map.connect 'show/:company_name/:id',
:controller => 'companies',
:action => 'show'
Basically, each time the show action is called, I would like it to take the company_name param and place it into the url as such (show/:company_name/:id)
However, it seems I am using old (rails 2.x routing api) and cannot use map.connect without getting an error. How can I upgrade this?
Is there some way to do this with "match"?
Thanks!
===================
This is the error I see when I try to use map.connect:
undefined local variable or method `map' for #<ActionDispatch::Routing::Mapper:0x103757458>
I think your routes lack a "/" symbol in the first line.
Try this:
match '/show/:company_name/:id' => 'companies#show'
You can check your routes path with command rake routes.
--
Besides, the show action is the default RESTful method in Rails. I'll suggest you change a equivalent word, and reserve "show" action for future or other situation.
In Rails convention, you can write resources :companies, and the path will be /companies/:id using show action.
Some adjustment, in app/models/company.rb
def to_param
self.name
end
So your url will look like http://yourdoamin.com/companies/37signals.
In app/controllers/companies_controller.rb
#company = Company.find_by_name(params[:id])
If I'm understanding your goal, try
match 'companies/show/:company_name/:id' => 'companies#show'
I am using Rails 3. And I'm wondering how to pass params to some blocks in routes.rb.
What I'm trying to do is to make a catch all route, that check from slugs database the model name of it by the id.
After getting the model name i pluralize it to get the controller name.
match '/:id', :controller => proc { Slug.find_by_iid(params[:id]).model.pluralize }, :action => :show
The table slugs
model iid
----- -----
post 4d2c7de0c5abe7f8a9000007
item 4d2c7de0c5abe7f809000004
When I try to access some pages like /4d2c7de0c5abe7f8a9000007 I got this error:
Started GET "/4d2c7de0c5abe7f8a9000007" for
127.0.0.1 at 2011-01-12 00:04:31 +0200
ActionController::RoutingError (wrong constant name #<Proc:0x0000010337c310#):
Rendered /Users/amr/.rvm/gems/ruby-1.9.2-p136#rails3/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)
The expected is to point to posts#view with iid: 4d2c7de0c5abe7f8a9000007
proc returns a Proc, but match is expecting a string. You could try adding .call to have the proc return its value. Though I'm not sure if this will end up calling the proc each time or only when routes is loaded...
EDIT
Seems I was way off-base with my earlier response and comments. Maybe something like this?:
match '/:id', :to => proc { |env|
id = env["action_dispatch.request.path_parameters"][:id]
model = Slug.find_by_iid(id).model
controller = [model.pluralize.camelize,"Controller"].join.constantize
controller.action("show").call(env)
}
Though this really ought to be defined in a library and included. Perhaps someone knows a better way?
Putting this in your routes seems really hacky. I would recommend creating a Slugs controller, passing this task onto that, and redirecting to the appropriate controller from there. Assuming your other pages use standard RESTful routes, you could do something like this:
Change route to this:
match '/:id', :controller => :slugs, :action => :show
Slugs controller:
def show
slug = Slug.find_by_iid(params[:id])
redirect_to send("#{slug.model}_url", params[:id])
end
In my view I specify a named route like this:
show.html.erb ->
seo_path(:id => "45")
Now in my routes I define like so:
routes.rb ->
map.seo "/pages/:id/:permalink", :controller => "pages", :action => "show"
Below is the error message I am getting. Apparently the diff is the id, although I don't know why.
Update:
I am getting this as my error:
seo_url failed to generate from {:controller=>"pages", :id=>"45", :action=>"show"}, expected: {:controller=>"pages", :action=>"show"}, diff: {:id=>"45"}
Why not using the to_param method?
class YourModel << AR::Base
def to_param
"#{id}-#{title.parameterize}"
end
Thus getting routes like
/pages/1-here-is-my-converted-article-title-to-permalink
Besides that, it would seems Rails isn't recognizing your route. Have you restarted your web server? Try sending only a :permalink attribute to see how it goes.
I don't fully understand your question.
It looks like id is already specified in your named route, and in your view. It looks like you need to also specify permalink in your view. Are you getting an error?