rails routing show action nested resources - ruby-on-rails

Yeah the problem is, that I created a nested resource like this:
resources :albums do
resources :elements
end
and the rake routes command displays:
album_element GET /albums/:album_id/elements/:id(.:format) elements#show
So when I am at
.../albums/1
I can head for
.../albums/1/elements
This starts the index action of the elements controller, just fine.
But if I edit the index.html.erb to
<%= link_to 'Show', album_element_path %>
I got an error like this:
Started GET "/albums/1/elements" for 176.221.47.67 at Tue Oct 09 14:25:39 +0200 2012
Processing by ElementsController#index as HTML
Parameters: {"album_id"=>"1"}
Rendered elements/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 123ms
ActionController::RoutingError (No route matches {:controller=>"elements", :action=>"show"}):
app/views/elements/index.html.erb:29:in `_app_views_elements_index_html_erb___13604879__168097178'
app/views/elements/index.html.erb:18:in `each'
app/views/elements/index.html.erb:18:in `_app_views_elements_index_html_erb___13604879__168097178'
app/controllers/elements_controller.rb:7:in `index'
So it says that no route matches ... but I actually have that in my rake routes displayed ?
What am I doing wrong ?

You need to provide the two neccesary arguments for album_element_path:
<%= link_to 'Show', album_element_path(#album, #element) %>

Related

ember/rails app loading not loading properly (takes too long) in development

i have a new ember-cli app with rails backend. it is taking about 30+ seconds to load in development... which is obviously way too long.
i am using the ember-cli-rails gem https://github.com/rwz/ember-cli-rails
i'm using the rails server (i've tried webrick and thin). i'm not using ember server, not running them separately.
i am not using the rails asset pipeline. i have deleted it.
here is my rails config/routes
Rails.application.routes.draw do
resources :rewrites, contraints: { format: :json }
resources :users, contraints: { format: :json }
resources :movies, contraints: { format: :json }
root 'application#index'
namespace :api do
get :csrf, to: 'csrf#index'
end
get "/*path" => "application#index", contraints: { format: :html }
here is my application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Better Films</title>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
here is my application/index.html.erb, frontend is the name of my ember app
<%= include_ember_script_tags :frontend %>
<%= include_ember_stylesheet_tags :frontend %>
when i go to localhost, my rails server log shows this
Rendered application/index.html.erb within layouts/application (173.6ms)
Completed 200 OK in 203ms (Views: 202.6ms | ActiveRecord: 0.0ms)
Started GET "/assets/frontend/frontend.css?body=1" for 127.0.0.1 at 2015-01-15 23:13:28 -0600
Started GET "/assets/frontend/vendor.css?body=1" for 127.0.0.1 at 2015-01-15 23:13:39 -0600
Started GET "/assets/frontend/frontend.js?body=1" for 127.0.0.1 at 2015-01-15 23:13:49 -0600
Started GET "/assets/frontend/vendor.js?body=1" for 127.0.0.1 at 2015-01-15 23:14:00 -0600
web inspector network tab shows
I'm not sure if this is related, but I had the same issue when I set the ember app's path in config/initializers/ember.rb to Rails.root.join('frontend').to_s. When I removed the path option, everything worked fine.

Turning resources into custom routes (Ruby/rails)

Started building an application here. client and server style architecture sending active resources across the wire and storing as activeRecord server side. Managed to get it up and running with a nice example in an O Reilly book except its using scaffold.
Rails routing - custom routes for Resources
is using map.resources from rails 2-
I'm using rails 3 so Its not really applicable and while I did post a question about routes from 2 to 3, I still cant convert this.
So here whats im looking at:
Rake routes with
resources :user_requests
gives:
user_requests GET /user_requests(.:format) {:controller=>"user_requests", :action=>"index"}
POST /user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
new_user_request GET /user_requests/new(.:format) {:controller=>"user_requests", :action=>"new"}
edit_user_request GET /user_requests/:id/edit(.:format) {:controller=>"user_requests", :action=>"edit"}
user_request GET /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
PUT /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"update"}
DELETE /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"destroy"}
I'd like to remove this and the resources and have my own routes pointing to my own defs.
Heres a quick attempt
match '/user_requests(.:format)' => 'user_requests#create , :via =>:post'
match '/user_requests/:id(.:format)' =>"user_requests#show"
returns almost the exact same as above
/user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
/user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
With the exception of the REST nouns at the start and the links. Its the same yet my own routes dont work.
What do I need to add to my routes to make them do the same thing as resources?
I'm not keeping scaffold as I've been told its never used in the real world. And I will be changing the names of my defs, but one step at a time.
Error that server shows:
Started POST "/user_requests.xml" for 127.0.0.1 at Tue Jul 12 17:13:32 +0100 2011
Processing by UserRequestsController#create as XML
Parameters: {"method"=>"POST", "user_request"=>{"depth"=>3000000, "url"=>"www.stackoverflow.com"}}
SQL (0.1ms) SELECT 1 FROM "user_requests" WHERE ("user_requests"."url" = 'www.stackoverflow.com') LIMIT 1
AREL (0.3ms) INSERT INTO "user_requests" ("updated_at", "depth", "url", "created_at") VALUES ('2011-07-12 16:13:32.765392', 3000000, 'www.stackoverflow.com', '2011-07-12 16:13:32.765392')
Completed 404 Not Found in 17ms
ActionController::RoutingError (No route matches {:controller=>"user_requests", :id=>#<UserRequest id: 6, url: "www.stackoverflow.com", depth: 3000000, created_at: "2011-07-12 16:13:32", updated_at: "2011-07-12 16:13:32">, :action=>"show"}):
app/controllers/user_requests_controller.rb:19:in `create'
app/controllers/user_requests_controller.rb:16:in `create'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
It would guess that line 19 of your UserRequestsController has something like
redirect_to #user_request
which tries to guess a URL for showing a UserRequest object. Rails no longer knows how to do this; you've lost the helper methods generated by resources (such as user_request_path, new_user_request_path, etc).
You can tell Rails to generate the "show" helper method by adding a :as option to your show route, without the _path postfix:
match '/user_requests/:id(.:format)' => "user_requests#show", :as => 'user_request'
You'll now have access to a user_request_path and user_request_url, which Rails can use to find the URL to "show" a UserRequest.

Rails routing error on valid route

I have the following in my routes file:
resources :timelogs do
member do
post :stop
end
collection do
get :start
end
end
which produces the following on 'rake routes' :
rake routes | grep stop
stop_timelog POST /timelogs/:id/stop(.:format) {:action=>"stop", :controller=>"timelogs"}
However, when posting a request to that URL I'm seeing:
Started POST "/timelogs/325/stop" for 188.220.17.64 at Wed Nov 24 02:22:22 -0800 2010
ActionController::RoutingError (No route matches "/timelogs/325/stop"):
All of this looks like it should be working, however, it's not. What could be the problem here?
I see no problem with the routes you've pasted and have verified that they work for me in an scratch app.
Started POST "/timelogs/123/stop" for 127.0.0.1 at 2010-11-24 11:49:25 +0000
Processing by TimelogsController#stop as */*
Parameters: {"a"=>"b", "id"=>"123"}
Rendered text template (0.0ms)
Completed 200 OK in 60ms (Views: 59.9ms | ActiveRecord: 0.0ms)
Perhaps something else in your routes.rb is in conflict here?
Actually when you are trying to send your form with exists resource (ticket) rails by default will send PUT request, so you should set :method => :post clear or change route from
post :resolve, :on => :member
to
put :resolve, :on => :member

My route returns a blank view (no html when I do view source)

I am brand new to Ruby on Rails and I have been trying to get a simple default route set up and working. When I try to run my application I get a blank result (if I do a view source, there is nothing there).
Here are the relevant files (not sure if I am missing something that would be useful).
app/config/routes.rb
Blog::Application.routes.draw do
root :to => "home#index"
end
app/views/home/index.html.erb
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Benji</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
When I try to run my application, I go to http://localhost:3000 and nothing shows up. If I do a view source, it is empty.
If do rake routes this is the result:
JESSE-GAVINs-MacBook-Pro-17:benji jesse$ rake routes
(in /Users/jesse/Dev/benji)
root /(.:format) {:action=>"index", :controller=>"home"}
JESSE-GAVINs-MacBook-Pro-17:benji jesse$
In my development.log file I see this:
Started GET "/" for 127.0.0.1 at Tue Sep 07 10:44:10 -0500 2010
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (2.8ms)
Completed 200 OK in 15ms (Views: 14.7ms | ActiveRecord: 0.0ms)
What could be the issue? How do I go about solving this?
Solved (sort of...)
It works just fine when I started the rails server on a different port.
It was my understanding that I didn't need to restart the server in order for it to detect changes in the code.
Sorry about the waste of time, I just didn't know which tree I needed to bark up. I learned some things along the way.
As you're playing with Routes, you absolutely need to restart the server. I don't know if this is part of the problem, but most (all?) things under config/ need the server to be restarted to take effect, particularly routes and environment

Why is my Rails web app calling the wrong action?

I'm diving into Ruby on Rails and I'm experiencing a weird bug, using Rails 3.0.1 and Ruby 1.8.7. Using the generated scaffold code, my "Show" action is getting called when I'm expecting my "Destroy" action to get called. Here's the code...
routes.rb
webappdotcom::Application.routes.draw do
resources :projects
root :to => "home#index"
end
index.html.erb
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>
actual HTML code that is rendered in browser
<td>Destroy</td>
server output when I click on the "Destroy" link
Started GET "/projects/12" for 127.0.0.1 at Wed Oct 20 23:39:37 -0500 2010
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"12"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE ("projects"."id" = 12) LIMIT 1
Rendered projects/show.html.erb within layouts/application (9.0ms)
Completed 200 OK in 77ms (Views: 13.3ms | ActiveRecord: 0.1ms)
You can see the "ProjectsController#show" action is being called, but I want the "Destroy" action to be called. Also, I noticed the browser isn't displaying the confirmation message "Are you sure?" either. Any ideas what could be causing this or what I'm missing?
Thanks so much!
This is because Rails 3 changed the way it uses javascript in forms. It used to add an onclick method to the link, but that goes against unobtrusive javascript. Now it just sets some tag attributes, and hooks into it with javascript in another file.
That page's layout (which is probably application.html.erb unless you've changed it) needs to have this line in the head section:
<%= javascript_include_tag :defaults %>
If this line is missing from your layout, that's the problem. Another possible cause could be if you've added the jQuery library to your app without adding the rails-specific jQuery library. Let me know how this works.
This may help you link_to with :method=>:delete not working ( Rails 3 ). Although in that article the edit is getting called instead. But the solution should work for show as well.

Resources