Error using singular form of model as a view - ruby-on-rails

I have a model "affiliate" and using the resources for resources :affiliates
I want to have a page site/affiliate so I added get '/affiliate', to: 'affiliates#affiliate' to routes.rb
When I go to /affiliate it shows a form and when I go to /affiliates it lists the affiliates. But now, every time I call #affiliate it changes the address to /affiliate.id example: When i click on the link show <td><%= link_to 'Show', affiliate %></td> it goes to the singular form .id instead of plural /id /affiliate.5
Is that a way to fix or I will have to change the view /affiliate to something different?

When you use a record in link_to, Rails need to figure out the path from the record. One of the things it does is to call to_param, which gives the id of the record by default. Usually it'll be able to match /affiliates/id. But the matching is not unique, and it can match some other possibilities too (I'm not sure about all the possible matches when a url is extracted from a record). What's important is that Rails will take the first matching route. In your case it is able to match /affiliate.id first since you have
get '/affiliate', to: 'affiliates#affiliate'
It's a legitimate url, with id as the format.
One thing you can do is to move this route after resources :affiliates in routes.rb, so Rails will match affiliate_path first. Or just use the named route in your link_to to remove the ambiguity.

It should be
<td><%= link_to 'Show', affiliate_path %></td>
or
<td><%= link_to 'Show','/affiliate' %></td>
Reason being your have mentioned resources :affiliates which generates url as per REST so
affiliates GET /affiliates(.:format) affiliates#index
so when you go /affiliates it will go to index action which gives you lists of affiliates

Related

Creating link_to for sequential nested resources

I am creating a rails app whereby from a record's "show" page, the user is able to cycle through the record's nested resources (similar to a slideshow). For example, from the "show" page, the user will be able to link to the "show" for the first nested resource. From there, the user will be able to link to the "show" page of the next nested resource and so on. As the ids of each nested resource should be ordered smallest to largest, how can I create a link_to that looks for the next highest id (assuming nested resources are being created for multiple records simultaneously they may not necessarily be sequential) of a nested resource within a given record.
Because of Rails magic, you can pass the resource directly to the route helper, and it will use the correct id for that resource. For example:
<% #foo.bars.each do |bar| %>
<%= link_to bar.name, foo_bar_path(#foo, bar) %>
<% end %>
The above assumes that your route file looks something like:
resources :foos do
resources :bars
end
I highly recommend Rails Routing from the Outside In; it's been a very helpful resource for me!
To set the order of the child resource, you could use a scope, like this:
class Bar < ActiveRecord::Base
scope :ordered, -> { order(id: :asc) }
end
And then in your view, call foo.bars.ordered.each do |bar| etc.... Your nested resource will be returned from lowest to highest ID, skipping over any that have been deleted.
I hope this helps with what you were asking.
EDIT
I misread the question. To dynamically generate the "next" id, you could create method, next, on your child class. This answer seems to be something like what you want. Then in your view, you can just call:
<%= link_to "Next", bar_path(current_bar.next) %>

default route in rails

I had a doubt when i was writing rails code.
In my link_to i used my route order to show my order. So:
<% #orders.each do |order| %>
<tr>
<th><%= order.name %></th>
<th><%= link_to 'Mostra', order %></th>
</tr>
<% end %>
I saw my rake routes and there was a :
order GET /orders/:id(.:format) orders#show
If i remember right i generated Order resource with scaffolding. However , when i created by hand new resources (not using scaffolding)
i had a different route for my resource. For example , i have something like name_resource_show(:id) for the show. This kind of style is good cause i understand that i have to pass the id , if i want to see a specific resource. But in the case before , the case of order , i really don't know how rails is able to understand to use the id of the object order. And also:
why i have different routes name? why i have sometimes _path and sometimes (maybe when i generate resource with scaffolding) other things?
i would expect something like order_show(:id) and not simply order.
how it works?
Rails helpers are smart enough to use model object to form url.
<%= link_to 'Mostra', order %> equivalent to <%= link_to 'Mostra', order_path(order) %> and both points to order show page.
This will generate 7 routes for your controller orders.
resources :orders
order GET /orders/:id orders#show
Here order is the helper method it provides to call routes instead of using /orders/:id.
Simply you can use order_path(order) to get route /orders/:id
Similary we get helper for all 7 routes. You can also override the helpers.
Go to below link for more information.
Reference: http://guides.rubyonrails.org/routing.html
First, I recommend following the Rails conventions on routes (see the main reference article here).
Here are answers to your questions in order.
The route you got from rake routes makes sense in the following way. Look at the URL (orders/:id). Within all of your orders, the :id passed specifies which one to look at. The GET nature of the request indicates you are getting the data on that record, i.e. that it is a SHOW action.
Rails understands where the ID is because of how the routes are structured. If you had order GET /orders/:year/:id in routes, then Rails will know to look for the third parameter for the ID it needs.
The two for accessing routes options are _path and _url (see here for details), but there are some alternatives explained in the main reference article I linked at top.
You can still use the explicit route, but the order option is simply a bit of sugar Rails offers to make things easier to read.

edit_article_path how does this work in the Rails view?

in section 5.11 of this rails 4 tutorial. I see this in my view
<td><%= link_to 'Edit', edit_article_path(article) %></td>
edit_article_path which goes to app/views/article/edit.html.erb
and i have a edit method in my article controller. Is this just Rails Magic (will it work in rails 3 too?)
Ie could i do foo_article_path(article) and it would redirect to a app/views/article/foo.html.erb and hit the foo method in my controller ?
I couldnt use this notation to redirect to a page from a different controller correct?
These helpers are created as part of the route definitions. See the following for the details
http://guides.rubyonrails.org/routing.html
For example you probably have something like
resources :articles
in your config/routes.rb file

Why is link_to with an absolute url considered technically superior to targetting controller action?

New to rails, so if this is discussed somewhere, just link me off: I had a good search but all I could find were people trying to figure out how to use link_to, not any discussion of this comment:
link_to "Profile", profile_path(#profile)
# => Profile
in place of the older more verbose, non-resource-oriented
link_to "Profile", :controller => "profiles", :action => "show", :id => #profile
# => Profile
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
I get that the latter is more verbose, and thus undesirable, but the former seems like a strange thing to be recommending.
If I have an action at say: /blah/add and I link to it using:
link_to "Link", link_add_path
Then I'm linking to mysite.com/link/add. This is a hard coded url.
If I change the route that this maps to, I have to change every instance of link_to in my code base to point to the new absolute url. This seems crazy.
However, if I link to it using:
link_to "Link", :controller => "thing", :action => "add"
Then the url is dynamically determined. If I have to change the path all I do is edit config/routes.rb and not touch any of my code. This seems like much lower maintenance.
I appreciate it's slightly more complex than that, the blah_path variable is not actually a static route, and actually contains some smarts like the application base url and prevents you from linking to urls that don't exist, but it seems like a step backwards to facilitate a fractionally less verbose syntax.
So, what's up with that?
What technical reason would you choose the former link_to syntax over the latter?
"You're doing it wrong" :P
Seriously though: use named resources, and here's why that's cool:
Example:
you've got this in your routes file:
resources :user_orders
And you are using "user_orders_path" everywhere. Then you do a refactor and decide (because the orders are now generic) that you want the controller to be called "orders" but you don't want to break all your old code. you can do this:
resources :user_orders, controller: "orders"
And your existing links will continue to work! (plus you can add a "orders" resource to move things over to the new scheme)
There's also neat things like named links:
match 'exit' => 'sessions#destroy', :as => :logout
I'd also like to add, if you needed to refactor your controller using the old link syntax - you'd still have to change a pile of controller links!
Then I'm linking to mysite.com/link/add. This is a hard coded url.
No, it's not. link_add_path is a method generated by Rails that points to a specific route in your config/routes.rb. You can see this by running
rake routes | grep link_add
If I change the route that this maps to, I have to change every instance of link_to in my code base to point to the new absolute url. This seems crazy.
No, you don't. Take the following example
get "link/add", as: :link_add, controller: :links, action: :add
If I run the above
rake routes | grep link_add
I get
link_add GET /link/add(.:format) links#add
But what if I change the name of my controller to UrisController? Just change the route in config/routes.rb
get "link/add", as: :link_add, controller: :uris, action: :add
and now you have
link_add GET /link/add(.:format) uris#add
The link_to's don't have to change because the link_add_path method is still mapped to the newly modified line in my config/routes.rb because the route name is the same. With your more explicit way of specifying controllers/actions for every link_to, you have to go through every link and update it manually to reflect the new controller: :uris controller.
Read about Naming Routes in the rails guide.

Multiple options for update action

How can I pass and collect different options into a controller action.
E.g you have a Team model and you want to add or remove Users from the team?
I would assume this would go in the update action of the teams controller, but the update action also need to be able to update team details like name, address, ect.
I tried the following code but that produce some weird results to my css and produces errors.
link_to team_path(user), params[:add] ,:class => 'btn btn-mini pull-right', :method => :put
Weird results are probably caused by the mixed parentheses
params[:add}
what does your model look like? (Teams-Teammember relation?)
But in general:
- you should add actions to the appropriate controller (prob. teams_controller) for
adding and deleting members:
def add_member
end
def remove_member
end
and define routes in config/routes.rb to be able to use this actions (there are plenty of examples how that can be achieved in the comments generated), then you can use the resulting path helper for your link_to tag - check out the available routes and path helpers with
rake routes

Resources