Ruby on Rails Actions help - ruby-on-rails

I have my index page which posts a single entry instead of the usual scaffold default of all of the entries. I told it to link to an action and it just responds to "Couldn't find Post with ID=all". It is the same as the default index method and index view. I assume this has something to do with routing but being no I have no clue. Any ideas?

The "all" name is misleading. If you want a page to display all the posts then the index page is perfect for that. If you want to show a subset of the posts then I recommend adding another action to your controller with a better name and then this to the routes.rb file:
map.resources :posts, :collection => { :some => :get }
Which you then can reference by using some_posts_path or some_posts_url.
For more information read the Official Ruby on Rails Routing guide.

Related

Using Rails 2.3.8 I am trying to create a new view for a child object

So I have a pretty basic issue I guess. I'm using Ruby 1.8.7 and Rail 2.3.8 b/c the web host is pretty out of date. Anyways, I have an Event which has RSVPs associated with it. I have an administrator that I want to have access to a better view (more information_ of the RSVPs for the event.
The RSVP currently indexes like normal /events/1/rsvps which I get to using event_rsvps_path(event) and use an index.html.erb file. I made a adminindex.html.erb and put an adminindex in the rsvp controller. But now I don't know how to create a link to that adminindex.html.erb file.
The usual methods like link_to :controller=>'rsvps', :action=>'adminindex' don't work for obvious reasons to me.
The Routes file has map.resources :rsvps, :except => :update
Can someone tell me how to link the index to the adminindex file if the admin is signed in?
The problem is in your routes.rb file. Creating a resource only makes routes for the 7 RESTful actions (index, show, new, create, edit, update, destroy)
Since you have a custom action, you have to add a custom route for it. Try this:
map.resources :rsvps, :except => :update, :collection => { :adminindex => :get }
Run rake routes to see if it made the route correctly.

Understanding routing with rails

I am trying to make a stupid simple CMS for one of my clients using rails. I generated scaffolding for Page and have been successful at creating and rendering those pages, however, the routing is ugly.
Right now, if I want to view the home page, I get a url like this: example.com/pages/1
I'd like to accomplish 2 things:
How do I set the routing so that example.com automagically grabs the page named "home"
and
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
Q1:
How do I set the routing so that example.com automagically grabs the page named "home"
In `routes.rb:
root :to => '[controller]#[action]'
#'pages#home' for example, if your home page is in `pages_controller`
# and uses the `home` action
Q2:
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
match '/page/:name', :to => 'pages#some_name', :as => :some_name
this would generate the following in $rake routes:
some_name /page/:name(.:format) pages#some_name
when you link to (or redirect, or otherwise access) this page, you'd write
link_to "click this link!", some_name_path(#SomeModel.some_name)
To accomplish the first thing you need to open the file routes.rb which is in the config folder and add the following:
root to: "home#index"
I'm assuming you have a controller named home which contains a method called index and this is the one that displays the home page.
If you want to make it the same as example.com/pages then you would have to use
root to: "pages#index"
to make a general rule you need to use
root to: "controller#method"
Don't forget to remove the index.html from the public folder too.
I recommend you make the blog application presented here:
http://guides.rubyonrails.org/getting_started.html
It can help you understand more.
Here's a solution that assumes your controller is named pages_controller instead of page_controller.
Add these routes in config/routes.rb:
root to: 'pages#show', page_name: 'home'
get 'pages/:page_name', to: 'pages#show'
For the controller app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def show
#page = Page.find_by(name: params[:page_name])
end
end
Note: In rails4 the find_by dynamic finders have been deprecated... so if you're app is on 4 you should look into updating them. These docs have further details.
Also, if you're just trying to get static looking urls then I would definitely go with Marian Theisen's suggestion and use friendly_id.

Rails always routes an action to show action, and the action it self becomes the id

There's a strange behavior in rails I found recently related to routes and actions, specifically, it's on rails 2.3.5. I have a controller, let's call it Users. In the routes, I declare Users as a resources.
map.resources :users
And within the controller, I have the standard action: index, show, edit, update & destroy. Also, I added other action to fullfil certain requirements.
def generated_pdf_report
# do something
end
The problem is, when I go to page /users/generated_pdf_report, I get this on the console:
Processing UsersController#show (some timestamps) [GET]
Parameters: {"action"=>"show", "id"=>"generated_pdf_report", "controller"=>"users"}
As you can see, the server route the request to method show rather than to method generated_pdf_report. What's interesting, is that I have other controllers having similar action and is working fine.
The solution to the above problem is easy enough, make sure the added feed is above the resources:
map.feed 'users/generated_pdf_report', :controller => 'users', :action => 'generated_pdf_report'
map.resources :users
My question is: Anyone knows why rails behaves like that? The above solution is kind of sloppy, what do you think the best practices for such problem like one mentioned above.
As outlined in the Rails 2 routing guide, you can add collection routes like so:
map.resources :users, :collection => { :generated_pdf_report => :get }
When rails looks at
/users/generate_report
That is exactly the path it would use to show a user whose id was generate_report, so that is what it does, assuming you haven't told it otherwise.
A shorter alternative to what you wrote is
resources :users, :collection => {:generate_report => :get}
Which tells rails to map a GET to /users/generate_report to your generate_report action

Customizing map.resources in Rails

Suppose I have a Book model, which contains many Page models.
The routing for this would be as so:
map.resources :books do |book|
book.resources :pages
end
Following the Rails default on this quickly leads to problems. Suppose Book #1 has 10 pages. The first Page in Book #2 will have this route:
/books/2/pages/11
This is a pretty bad route, what would make more sense is this:
/books/2/pages/1
Or even this:
/books/2/1
Is there a way to still use map.resources, but get a result like this:
/books/{book.id}/pages/{page.page_number}
No. You have to use custom routing for that.
Feel free to get inspiration from http://github.com/augustl/kii/blob/master/config/routes.rb
As August says you need to use custom routing for that.
But for the pages, you don't need the full resources routes. Only show will be necessary.
So something like :
map.resources :books do |book|
book.page ':page_id', :action => 'index'
end
Will map the default books url for displaying the index, one book and adding/editing them.
But also a page
/books/{book.id}/{page_id}
Which maps to the index action with the parameter "page_id". You only have to display the appropriate books page ;)
You could also try the shallow-option for your routing!

rails routing problem

I have some RESTful controllers and have added a custom method to application_controller.
I have not changed anything in the routes to indicate this new method. The method is poll_memos. I have entered the following URL:
/groups/1234/poll_memos
I get the following error:
Unknown action
No action responded to 1234. Actions: create, destroy, edit, index, new, poll_memos, show, and update
Two questions: Since I didn't modify routes how does rails know about poll_memos? Second, since it seems to know about it, why is it not working?
I don't think thats a restful route that rails automatically generates. This means you'll need to add it yourself.
Look at this question and this one.
And its in the actions because its in the controller, the error message is just printing all actions.
The correct url is /groups/poll_memos/1234. In your example rails thinks that you're trying to call controller method named "1234", which is absent, of course.
Rails knows about poll_memos because the code that prints the error message looks at controller code, not to the routing. You may set routes up in such a way that it will say that there is poll_memos method, but you're unable to access it via URL.
This is because you are most likely triggering the default route:
map.connect ':controller/:action/:id'
Your URL
/groups/1234/poll_memos
would map as follows:
{:controller => "groups", :action => "1234", :id => "poll_memos"}
Also, as you are using a restful style you need to configure the route. To get the poll memos working on the items in the collection you'll need to modify your routes to map as follows:
map.resources :groups, :member => {:poll_memos => :get}

Resources