I have the following url /purchases/3/payments/new, which I get in the purchases controller by
link_to 'pay', purchase_path(purchase)+new_payment_path
Now in the payments controller I would need to have the purchase object, or its id at least from where it was invoked.
I tried using a param, but is there any other way ?
Thanks
Using params makes sense.
You should be able to get the purchase ID like this in the payments controller:
params[:purchase_id]
However, need to setup your routes in a specific way to do this:
resources :purchases do
resources :payments
end
Then you can create the link in the view like this:
link_to 'pay', new_purchase_payment_path(purchase)
Have a look at these docs too: http://guides.rubyonrails.org/routing.html#nested-resources
Routes
I immediately highlighted this as a problem:
purchase_path(purchase)+new_payment_path
This is really bad (configuration over convention) - you'll be much better using the actual path helper to load the resource you need (keeps it DRY & conventional)
--
Nested
As mentioned by Jon M, your solution will come from the use of nested resources:
#config/routes.rb
resources :purchases do
resources :payments # -> domain.com/purchases/:purchase_id/payments/new
end
This will allow you to use the route path as described by Jon.
--
Controller
in the payments controller I would need to have the purchase object,
or its id
By using nested resources, as described above, the route will hit your payments controller and have the following param available:
params[:purchase_id]
Note the naming of the param (only the nested resource is identified with params[:id]), as demonstrated in the docs: /magazines/:magazine_id/ads/:id
I would recommend using the following code in your controller:
#app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
def new
#purchase = Purchase.find params[:purchase_id]
end
end
Related
I'm trying to understand the best way to architect a fairly simple relationship. I have a Job Model and a Category Model with a has_many relationship between them in a JobCategories model.
I'd like to have a page that lists all Jobs for a specific Category. Should the logic to pull this data be on the Category Controller (on the show action), or should I create a category method on the Job Controller? My gut tells me it should be on the Category side because a Category has Jobs, but it doesn't feel right that a Job would have the logic to pull all the Jobs for a given category.
Having said that, if I want the URL to be something that is more Job specific like:
domain/jobs/:id/{category-name} (for SEO purposes)
How would I structure the route so that it reads like the above, as opposed to
domain/categories/:id
which is what you'd get with resources :categories, only: [:show]
Thanks!
For a pretty slug, I'd suggest using FriendlyId on your categories model.
As for the routes, you will not get the desired route using resources :categories
One way to do it would be
resources :jobs, only: [] do
member do
get '/:slug' => 'categories#some_action'
end
end
the slug will be passed in your parameters.
This will yield a route like this
GET /jobs/:id/:slug(.:format) categories#some_action
UPDATE
the :slug is just an example for pretty url.
In your case you'd want to have :category_name. That would be passed into your controller through the params[:category_name].
One thing that I did start thinking when I re-read your question is that you want to show a list of jobs for a specific category. A url path like /jobs/:id/{category-name} shouldn't actually show a list of jobs as you are specifying an id which means a specific job. I think the url that you're looking to get is more along the lines of /jobs/{category-name}. Am I correct?
UPDATE 2
I suggest you read this Ruby On Rails Routing
UPDATE 3
Since you did want an url more like /jobs/{category-name}
You're routes should look like this
resources :jobs, only: [] do
collection do
get '/:category_name' => 'categories#some_action'
end
end
Good luck with your project! :D
I have a CRUD resource defined in my routes.rb file: resource :user.
I'm adding a new controller method for the user called search_places, which is performed on the user to find other users with the same places. I'm adding a route it.
Right now, I have:
post '/user/search_place', which isn't very DRY. I'm new to Rails and I was reading the Rails routing documentation and figured that I could possibly use
resource :user do
collection do
post 'search_place'
end
end
Is this considered good practice? I know this works (it passes my rspec route test), but is that how its best done?
Thank you,
When you add second don't need of first.
Add this:
resources :user do
collection do
post 'search_place'
end
end
Remove this:
resources :user
That makes DRY :)
Suggestion: Resources name should be defined in plural if u follow rails convention. (i.e) resources :users
In my app I have a User model which defines a history method that returns a list of Activity objects, showing the last N actions the user has carried out. The UserController#history method wires this with a view.
The code looks as follows:
class UserController < ApplicationController
def history
user = User.find(params[:id])
#history = user.history(20)
end
end
class User < ActiveRecord::Base
has_many :activities
def history(limit)
...
end
end
Naturally, I also added this line to my routes.rb file:
match '/user/:id/:action', :controller => 'user'
so now when I go to localhost:3000/user/8/history I see the history of user 8. Everything works fine.
Being a Rails NOOB I was wondering whether there is some canned solution for this situation which can simplify the code. I mean, if /user/8 is the RESTful way for accessing the page of User 8, is it possible to tell Rails that /user/8/history should show the data returned by invoking history() on User 8?
First of all the convention to name controllers is in the plural form unless it is only for a single resource, for example a session.
About the routes I believe you used the resources "helper" in your routes, what you can do is specify that the resource routes to users also has a member action to get the history like this
resources :users do
member do
get :history
end
end
I think there is no cleaner way to do this
You can check it here http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
As far as the rails standards are concerned, it is the correct way to show the history in your case. In rails controllers are suppose to be middle-ware of views and model, so defining an action history seems good to me.
And you can specify the routes in better way as:
resources :user do
get 'history', :on => :member #it will generate users/:id/history as url.
end
I have a question about browser string in rails.
For example i have rails app with routes:
resources :posts
and this resource create :
post/:id
post/21
post/167
post/356
but i create a simple blog and i want to rename ':id' to
post/some-name
post/another-name
post/another-different-name
in post i have title, text field
but i dont know how do this
I know that this can be achieved through manipulation of the :id
can you post some link with detailed answer on this question, or some simple example
You can of course put anything you want in the URL and actually there is railcast about it:
http://railscasts.com/episodes/63-model-name-in-url
It is preferable (read: easier) to also keep model.id in the URL, or it means that post name MUST be unique, otherwise you can put anything you want:
/post/2465-my-pretty-post-name
Also, there is a gem friendly_id and related railcast:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Hope that helps.
Why do you want to change /post/:id ?
You can achieve something like /post/:id/comments
You can do that using nested resources like this in your routes.rb
resources :posts do
resources :comments
end
Check here for more details
http://guides.rubyonrails.org/getting_started.html
If you add the to_param method to the model then you can use that within your URL system.
class SomeModel < ...
def to_param
self.title
end
end
Then inside your controller, setup a filter to fetch the model using the title attribute instead of the ID attribute which is used for the find method.
before_filter :setup_record
def setup_record
#record ||= Record.find_by_title(params[:id])
end
You will have to ensure that your title stays unique and if you change it then you will either have to discard all other previous URLS or keep a history of older names.
I'm working on a rails app and using a singular resource. However the controller name for the singular resource is plural.
Eg map.resource activity_report expectes the activity_reports_controller.
The explanation given in the rails 3 guide is: "... you might want to use the same controller for a singular route and a plural route..." That is a reasonable explanation, but what is the use case for using the same controller to handle a singular route and a plural route?
In a RESTful Rails application there is usually a mapping of one controller per RESTful resource. For example, let's say we wanted a controller to process user logins (/session) but also to provide a list of users who are currently logged in (/sessions). Logically we could put both of those responsibilities within a SessionsController:
class SessionsController < ApplicationController
# GET /sessions
# Display a list of logged in users
def index
...
end
# GET /session/new
# Display the login form
def new
...
end
# POST /session
# Authenticate a user
def create
...
end
end
An alternative would be to split the functionality for listing logged in users out into a separate administration controller.
You can use it.
class UsersController < Application
end
map.resource :user
map.resources :users
Another situation in which I can imagine using it would be, let's say (and this isn't necessarily the business model you'd want, but stay with me for a moment) you are going to make a site of film reviews, and film information. So, on the one hand you'd have the link to your list of the latest reviews be a plural resource route, something like this:
http://yoursite.com/reviews?count=5
So, in this case, you have a controller for the collection, right? But you're only going to review each movie once. So what if you wanted to provide an easy access to a movie's review?
http://yoursite.com/movies/pirates_of_the_carribean_2/review
Well, there's a nested single resource route, because a movie has_one review, right?