Rails 5 route, url issue - ruby-on-rails

I'm having an issue trying to get a link_to working after changing routes on my app. This may be a simple one but I honestly cannot see just linking to link_to with the object name will not work as previously did before the route changed.
Background: I'm using friendly_id gem (not sure if i still need this after making changes to the url manually).
I'm also using Ancestory gem in there too.
On the front facing app. I've a listings resource that is only used for index and show. See below routes and also previous working route.
# Front facing resources
scope "/:category_name/:category_id" do
resources :listings, only: [:show], path: '/:title'
end
resources :listings, only: [:index]
before (working fine)
# Front facing resources
resources :listings, only: [:index, :show]
resources :auctions, only: [:index, :show]
I've been successful making my url show a result of:
listing GET /:category_name/:category_id/:title/:id(.:format) listings#show
result:
http://localhost:3000/listings/helicopter/test-one/4
This will do for now. I'm still trying to figure the end url. So I can create the listing and view it my manually typing the url into the browser.
The error occurs when I'm viewing the Index page of the listings and I have a link_to
current code:
<%= link_to listing.title, listing %>
Error:
No route matches {:action=>"show", :controller=>"listings"}, missing required keys: [:category_id, :category_name, :id, :title]
What way do I go about adding these required keys to the link_to helper? I've tried lots of ways but not far. I presumed changing just the show route would not have messed too much with any previous controller methods and that the listing would still be found by id.
Any direction would be great. Thanks a million.
*** UPDATE ****
Ok so before sending the above I had a closer look and got it working however I'm asking is there a cleaner way to do this? It just doesn't look very "The Rails way". My end goal is to have a long SEO friendly link that contains category and possibly subcategories.
Working code:
<%= link_to listing.title, listing_path(category_name: "listings", category_id: listing.category.name, title: listing.title.parameterize, id: listing.id) %>
Result:
http://localhost:3000/listings/Aircraft/twin-piston-jet/1
Thoughts please???

You could use helpers for that https://api.rubyonrails.org/classes/ActionController/Helpers.html
app/helpers/listing_helper.rb
module ListingHelper
def listing_route(listing, category_name = "Listings")
listing_path(category_name: listing, category_name: listing.category.name, title: listing.title.parameterize, id: listing.id)
end
end
On your views
<%= link_to listing.title, listing_route(listing) %>

Related

Rails nested routes with link_to

I'm trying to use a link_to from my view into a nested route and I am sure I just have my syntax incorrect. The basic flow of my app is there is a summary that has many feeds which then have many log lines. its a very basic report
My route is
resources :perfsums, :only => [:index] do
resources :perffeedresults, :only => [:index] do
resources :loglines, :only => [:index] do
end
end
end
Here is how the rake routes looks for that
perfsum_perffeedresult_loglines_path GET /perfsums/:perfsum_id/perffeedresults/:perffeedresult_id/loglines(.:format) loglines#index
In my view I have a link to I want to link from the feed class to log lines using the feed ID. Should be very simple. My link to looks like this
<td> <%= link_to c.id, perfsum_perffeedresult_loglines(c) %> </td>
Going directly to the page by hand works as the link below shows I just can't get there form that link to
http://localhost:3000/perfsums/19/perffeedresults/143/loglines
When I try to run with that link_to I get. I have tried a few different options here none have worked.
undefined method `perfsum_perffeedresult_loglines' for #<#<Class:0x007fad31b60dd8>:0x007fad386d86b0>
I do use a link_to to go from the summary to the feeds page fine its just that extra bit of nesting t hats throwing me off I think.
First of all, it's a perfsum_perffeedresult_loglines_path, you missed _path, which should be added to get a path. And you need to pass two params, look at the path:
/perfsums/:perfsum_id/perffeedresults/:perffeedresult_id/loglines
You need to pass a perfsum and a perffeedresult:
<td> <%= link_to c.id, perfsum_perffeedresult_loglines_path(perfsum, perffeedresult) %> </td>

Rails 5 link_to unscoped path in scoped controller view

I got a strange issue
this is my routes.rb
get '/fin', to: 'pages#fin'
mount S::Engine => '/pod'
S::Engine.routes.draw do
resources :articles, only: [:show, :index], path: 'posts'
root 'articles#index'
not let say I go to my root path of the S engine. inside the view I'm trying to put a link to '/fin' so I write
<%= link_to "Fin", fin_path %>
But I get an unknown route error.
I also tried to use the controller:'/pages' to solve the controller resolving, but it didn't help
like this: url_for controller:'/pages',action:"fin"
The route exists, and the problem is related somehow to the scoping.
thanks,
Try: get 'fin', to: 'pages#fin' and then try to run rake routes in your terminal to see if fin_path is there. Also what do you have in your fin method in your pages controller?

ActionController::UrlGenerationError using link_to

I have a controller Posts in which I have a method:
def report_user
...
end
I have a view where I would like a link that will perform some logic (it should NOT actually take the user to a different page, only perform the logic and possibly show a dialog box after completion). The logic is contained in the report_user action in the Posts controller:
<%= link_to "Report User", :controller => :Posts, :action => :report_user %>
I would ultimately like to pass some variables also to the report_user action, however I haven't gotten that far as I've come across this error:
No route matches {:action=>"report_user", :controller=>"Posts"}
message << " missing required keys: #{missing_keys.sort.inspect}" unless missing_keys.empty?
raise ActionController::UrlGenerationError, message
end
I'm not sure what the issue is. There is definitely an action in the Posts controller called report_user and it is not private. I'm not sure what the missing required keys means either. I've seen on SO other people with that error, but they all have routes defined that require parameters. I do not have any routes defined for this. Possibly I"m going about the entire thing in the wrong way?
As Nils suggested, you need an entry in routes.rb. Assuming that this is a member route using a GET request, that entry would look like this.
resources :posts do
get :report_user, on: :member
end
Next, you need to update your link to use the routing helpers that Rails provides.
<%= link_to "Report User", report_user_post_path(#post), remote: true %>
I included the remote: true option b/c you mentioned that clicking the link shouldn't reload the page. The default response for this request will be app/views/posts/report_user.js.erb.
I would encourage you to read up on Rails routing at http://guides.rubyonrails.org/routing.html.

Rails 4 Routing - undefined local variable

I have a payments table. In the index view I list all payments in a table and in the show view I'd like to show a form of all payments where a user can select which ones to further process.
Above the table in the index action view I have:
<%= link to "Customise", show_payment_path %>
Then in the controller:
def show
#payments = Payment.all
end
In my routes file:
resources :payments
The error I am getting is:
undefined local variable or method `show_payment_path'
I have tried
<%= link_to 'Customise Sepa Mandate', show_payments_path %>
as well but that gives the same error. As does using url instead of path.
The path for resourceful-routed show actions is just payment_path (singular resource) or payment_path(id). Your “customize” link would lead me to believe you actually want edit_payment_path(id), though.
See Rails Guides — Generating Paths and URLs From Code for more info.
Run rake routes command to see available routes and correct syntax.
Pass show_payment_path(:id) to get the particular payment show page.
for that make your own routes and create their own path variable such as
resources :payments, except: [:show] do
get '/show' => "payments#show", on: collection, as: :show
end

Routing / in Rails

I'm writing a simple Rails app with one main controller and I want to map /(:action(:id)) to this controller (basically remove the controller prefix at the beginning), but still have the path helpers I get by using map.resources.
I tried map.root :controller => 'notes', but I get an error:
undefined method `note_path' for #<ActionView::Base:0x102038b50>
where I use the link_to_unless_current function in my view.
Edit: Here is the code in index.html.erb that gives the error.
<% for note in category.notes %>
<h3><%= link_to_unless_current h(numbered_title note), note %></h3>
<% end %>
UPDATE: I don't know how I came to this question, but author just pointed out that this is 3 years old, which I totally missed. I will leave this answer if somebody needs that behaviour in rails 3. In rails 2 it is not valid...
Route root will not work with whole controller as this has to point on specific action.
First parameter of resources will be used to determine path (normally it would be /notes) and at the same time to create helpers like notes_path. What you want to do is set this to '/', but also add :as option to give proper helpers names. So finally it should look like:
resources '/', controller: :notes, as: :notes
Also quite important thing to notice, if you want to use any other resources, you should put them above notes route. Otherwise rails will recognize resources name as id of notes action show.
Example:
resources '/', controller: :notes, as: :notes
resources :comments
Going to /comments will try to find note with id 'comments'.
resources :comments
resources '/', controller: :notes, as: :notes
Opening /comments will go to comments_controller#index.

Resources