Rails can't find Path - ruby-on-rails

So, I have the following code. I want to access timespans_path,
but I can't.
<% content_for :div_header do%>
<h1> Welcome, <%= #l_user.name %> </h1>
<% end %>
<% content_for :div_sub_header do %>
<ul>
<li><%= link_to "show entries", entries_path %></li>
<li><%= link_to "show groups", groups_path %>
<% if can? :read, Subgroup %>
,
<%= link_to " subgroups", subgroups_path %>
</li>
<% end %>
<li><%= link_to "show users", users_path %></li>
<li><%= link_to "show actioncodes", actioncodes_path %></li>
<li><%= link_to "show timespans", timespans_path %></li>
</ul>
<% end %>
I always get these errors:
NameError in Application#welcome
Showing C:/xampp/htdocs/fluxcapacitor/app/views/application/welcome.html.erb where line #16 raised:
undefined local variable or method `timespans_path' for #<#<Class:0x58b8610>:0x58b7e18>
This is my route.rb:
Fluxcapacitor::Application.routes.draw do
root 'application#welcome'
get 'login' => 'application#login'
post 'login' => 'application#process_login'
post '' => 'application#process_login'
post 'send_request_account_mail' => 'application#send_request_account_mail'
post 'send_forgot_password_mail' => 'application#send_forgot_password_mail'
get 'forgot_password' => 'application#forgot_password'
get 'request_account' => 'application#request_account'
get 'welcome' => 'application#welcome'
get 'logout' => 'application#logout'
if Rails.env.development?
get 'display_mail' => 'application#display_mail'
end
resources :users
get 'multiple_new' => 'users#multiple_new'
post 'multiple_new' => 'users#multiple_new'
post 'multiple_create' => 'users#multiple_create'
get 'users/:id/:hash/cal' => 'users#cal'
resources :actioncodes
resources :entries
resources :timespans
resources :groups do
member do
get 'search_admin'
post 'search_admin'
post 'add_admin'
get 'remove_admin'
post 'remove_admin'
end
end
resources :subgroups do
member do
get 'search_user'
post 'search_user'
post 'add_user'
get 'remove_user'
post 'remove_user'
get 'remove_admin'
post 'remove_admin'
end
end
end
Why am I getting the error? How can I fix it?

Add
resources :timespans
in your route.rb

It looks like you haven't defined a path called timespans, so the view doesn't know what URL to render and is raising an error.
If you have a model called Timespan then adding resources :timespans to your routes file will create a path called timespans_path (among others), pointing to /timespans.
You can also create any arbitrary path called timespans path using the :as option, e.g.:
get "/the_url", to: "the_controller#the_action", as: :timespans
If you have problems with paths in the future, note that you can use the rake task rake routes to list all your generated routes and the names of their path helpers, which can be very helpful for debugging.

Related

Create a 'resources' route for static_pages controller in rails 6.1.3

total newbie here ^^
So I have a bunch of static pages in my footer that I would like to create a route for.
Right now, I am using get to create the routes as such:
Rails.application.routes.draw do
root 'static_pages#home_page'
get 'static_pages/secret'
get 'static_pages/stripe_button'
get 'about_us', to: 'static_pages#about_us'
get 'rules', to: 'static_pages#rules'
get 'faq', to: 'static_pages#faq'
get 'community', to: 'static_pages#community'
get 'terms', to: 'static_pages#terms'
get 'privacy', to: 'static_pages#privacy'
end
Ideally, I am trying to condense all of that into a resources like so:
resources :static_pages, only: [:index] do
get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
end
Here is the footer li for the pages:
<div class="col">
<ul>
<li><%= link_to "About Us", 'about_us', class: 'nav-link d-inline-block' %></li>
<li><%= link_to "Rules", 'rules', class: 'nav-link d-inline-block' %></li>
<li><%= link_to "FAQ", 'faq', class: 'nav-link d-inline-block' %></li>
</ul>
</div>
<div class="col">
<ul>
<li><%= link_to "Community", 'community', class: 'nav-link d-inline-block' %></li>
<li><%= link_to "Terms", 'terms', class: 'nav-link d-inline-block' %></li>
<li><%= link_to "Privacy", 'privacy', class: 'nav-link d-inline-block' %></li>
</ul>
</div>
How do I get rid of all the gets and condense all of it into one resources?
ruby 2.7.1
rails 6.1.3
Thx a lot :)
First of all, resources is a concept that revoles around an actual resource, or record if you will. Your static routes don't really follow that, so you the way you did it is actually the right way to do it (see also here: https://guides.rubyonrails.org/routing.html#non-resourceful-routes).
If your sole purpose is refactoring your routes file, I wouldn't worry too much, in fact I would leave it as is because I think it's more important to follow the concepts.
If you still want to go ahead and you don't care too much what the actual urls look like you can do it like so:
resources :static_pages, only: [:index] do
collection do
get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
end
end
Urls will look like this then /static_pages/terms.
The collection makes sure there is no :id needed in the routes. You can always double check in the terminal with rails routes - also in order to find the prefix for the link_to helper (will be terms_static_pages_path).
You may find ideas there
Rails routing: resources with only custom actions
This could be your solution:
scope '/', controller: :static_pages do
get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
end
If you don't want to manually type all actions, you can use the class method .action_methods and exclude from it all actions for whom you need a specific route:
scope '/', controller: :static_pages do
(StaticPagesController.action_methods.to_a - ["home_page", "secret"])
.each do |action_method|
get action_method
end
end

Issue with link_to in index

i am a newbie to rail and try to build my first site but face an issue with a link_to in an index page. The link redirect to /recipes.1 instead of /recipes/1.
The show page work when i try /recipes/1.
Index.html.erb
<% provide(:title, "Recipe") %>
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe%>
<%end%>
route.db
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
resources :users
resources :recipes
recipes_controller.rb
def index
#recipes = Recipe.all
end
def show
#recipe = Recipe.find(params[:id])
end
Remove the following routes from routes.rb:
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
The above routes are not required since resources :recipes generate all these routes for you.
Hope it helps!
Try this code
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe_url(recipe) %>
<%end%>
Use
<%=link_to recipe.label, recipe_path(recipe)%>
Instead of
<%= link_to recipe.label, recipe%>
you have defined show path twice.
1. in manual defined path
get 'recipe' => 'recipes#show'
2. Through
resources :recipes
If you do rake routes, then you will first option create routes for get method with /recipe url and appends parameter to it which result in /recipes.1 path.
Also rails read routes file in top to bottom approach. As uses first routes for show method.

Strange Routing error in rails 3. Rails changes the route that is specified

I have a rails 3 app, and when I click the link to my terms page, it routes to a completely different controller, than what the routes should use. Stranger still, the route works when I'm not logged in, and I'm using devise.
I get this error when clicking the link when I'm logged in.
No route matches {:action=>"edit", :controller=>"users", :id=>nil}
<%= link_to "Terms", terms_path %>
Routes (in the order they appear in routes.rb):
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
member do
get :following, :followers
post :accept
end
end
match '/terms', to: 'static_pages#user_agreement'
Static Pages Controller
def user_agreement
end
Rake Routes
terms /terms(.:format) static_pages#user_agreement
This also happens for every other action that I've routed this way to the staticpages controller, but not for any other actions that route to different controller.
Update: Terms Page
Header:
<%= link_to "Follow", users_path %>
<%= link_to current_user.name, current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
Footer:
<%= link_to "Welcome", welcome_path %>
<%= link_to "Settings", edit_user_path(#user) %>
<%= link_to "Terms", terms_path %>
All the content is pure html.
Thanks in advance
You have a link to edit_user_path with no #user as hinted in the comments.
You should almost certainly be using current_user anyway.

link_to with Namespaces in Rails

I'm trying to get an admin interface working in Rails, but I'm having trouble using link_to with the nested routes. I'm trying to get a link to /admin/cake_class/:id but it's sending me to admin/cake_class.:id instead.
config/routes.rb:
namespace :admin do
resources :cake_class
end
/app/views/admin/cake_class/index.html.erb
<h1>all classes</h1>
<% #classes.each do |c| %>
<%= c.date %>
<%= c.name %>
<%= link_to 'Show', admin_cake_class_path(c) %>
<% end %>
Any suggestions?
The :resources should be plural, :cake_classes, which could be tripping you up.
What does the output of rake routes show?

In rails I enter the list action, it gives me a error about show action

Hey guys, I'm new to Rails. I'm very confused about this problem I have right now. When I hit the http://localhost:3000/videos/list it gives me an error about not specifying the show action.
Here's my code
# routes.rb
Drumvideo::Application.routes.draw do
resources :videos
end
# videos_controller.rb
class VideosController < ApplicationController
def list
#videos = Video.order("videos.updated_at DESC")
end
end
# list.erb.html
<% #videos.each do |video| %>
<ul>
<li><%= video.title %></li>
<li><%= video.desc %></li>
<li><%= video.tudou %></li>
<li><%= video.drummers.first.first_name %></li>
</ul>
<% end %>
I think the problem is in the resources routes, But I don't know exactly how to fix it.
change your routes.rb to
Drumvideo::Application.routes.draw do |map|
resources :videos, :collection => {
:list => :get
}
end
The list action is very old in Rails and is not use any more. Whatever is recommending that you use it is outdated and should not be trusted.
Instead, read the Getting Started guide for how to properly use Rails now: http://guides.rubyonrails.org/getting_started.html

Resources