Rails Forem Engine routes not working - ruby-on-rails

I'm trying to mount Forem in my rails application. I added it to my Gemfile and installed it properly. However, whenever I try and load a page under Forem's control (such as "/forums"), I get NoMethodError's on the links with non-Forem routes. These links are in a partial header included in my application.html.erb, called _header.html.erb:
<div id="nav">
<div id="nav-wrapper">
<%= link_to raw("<div>Home</div>"), :root %>
<%= link_to raw("<div>Forums</div>"), :forem %>
<%= link_to raw("<div>Events</div>"), :events %>
<%= link_to raw("<div>Applications <b class='carat'>▼</b></div>"), "#"%>
<%= link_to raw("<div>Reports <b class='carat'>▼</b></div>"), "#"%>
<%= link_to raw("<div>Tutorials</div>"), "#"%>
<%= link_to raw("<div>Rules</div>"), '#'%>
<%= link_to raw("<div>Roster</div>"), :roster %>
<%= link_to raw("<div>Donations</div>"), '#'%>
<%= link_to raw("<div>Media Center <b class='carat'>▼</b></div>"), '#'%>
<%= link_to raw("<div>L.O.A</div>"), '#'%>
</div>
routes.rb:
mount Forem::Engine, :at => '/forums'
resources :events
resources :news
devise_for :users
get 'roster' => "users#roster"
get 'news' => "news#index"
get 'profile/:user' => "users#profile", :as => :user_path
root 'news#index'
The error:
NoMethodError in Forem::Forums#index
undefined method `forem_path' for #<#:0x000000031583c8>
And so on for all the other links:
NoMethodError in Forem::Forums#index
undefined method `events_path' for #<#:0x000000031583c8>
NoMethodError in Forem::Forums#index
undefined method `roster_path' for #<#:0x000000031583c8>
This has to do something with the routes.rb's scope, but I'm not sure how I can get these links working again.

I have not used Forem but I had this same issue with Monologue. I think it will help if the links in your header specify that they are part of the main_app. Example:
Original link to root path might have been:
root_path
and now should be:
main_app.root_path
The reason for this is that the Forem Rails engine is a separate Rails application and the links without the prefix only work within the current Rails app (the main app, in this case).

Related

Facing a NoMethodError and unable to pinpoint the source

I added the devise gem to my rails app. Then I appended the url with user/sign_up as that is self created by the app. Then the following page shows up -
I am unable to locate the file with the following code.
This is what the bottom of the page looks like -
Do let me know what more information I can provide.
Thanks for your time and help.
EDIT 1 Added routes.rb for your reference
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts, only: :create
get 'contact-us', to: 'contacts#new', as: 'new_contact'
end
EDIT 2- Added the following piece of code to my application.html.erb file
<% if user_signed_in? %>
<%= link_to "Log Out", destroy_user_session_path, method: :delete, class: "btn btn-default navbar-btn" %>
<% else %>
<%= link_to "Log In", new_user_session_path, class: "btn btn-default navbar-btn" %>
<%= link_to "Sign Up", new_user_registration_path, class: "btn btn-default navbar-btn" %>
<% end %>
And a page displayed the error: NoMethodError in Pages#home(as in the image added) with the subsequent lines-
Showing /home/ec2-user/environment/saasapp/app/views/layouts/application.html.erb where line #31 raised:
undefined method `user_signed_in?' for #<#Class:0x000000047464c8:0x007f399e3e7ab8>
Did you mean? user_session_url
Did you follow devise docs for installing devise in rails? There is something to do with your routes.rb. Go through this article https://guides.railsgirls.com/devise and follow all the steps carefully. Also post your routes.rb maybe the problem is with your routes. Thanks, This type of question is asked here just try those step as well.
See this link as well.

No route matches [GET] "/users/country.html"

It is nice when I directly open 'http://localhost:3000/country.html' or click 'Country' button on navigation bar. But if I click 'Login'/'Signup'(http://localhost:3000/users/signin.html) first and then click 'Country' button, the url link to 'http://localhost:3000/users/country.html'. It shows 'No Route matches [GET] "/users/maison.html"'. Even more, the colour of 'Login' changes to a strange red which is similar to the default red '404' page in public.
Could anybody tell me why different clicking sequences will lead to different urls and how can I figure out the issue? Thanks!
This is an app based on Ruby on Rail, with devise, ruby-2.3.7,'rails', '~> 5.2.2' and mysql. I am using macOS Mojave.
'''
'application.html.erb':
<div class="outside_container">
<div class="main_container">
<div class="navbar clearfix">
<div class="container">
<ul class="nav left">
<li>Introduction</li>
<li>Country</li>
<li>Maison</li>
</ul>
<% if notice %>
<p class="notification"><%= notice %></p>
<% end %>
<% if alert %>
<p class="notification"><%= alert %></p>
<% end %>
<ul>
<p class="nav right">
<% if user_signed_in? %>
<%= link_to "Edit profile", edit_user_registration_path %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete %> |
<% else %>
<%= link_to "Sign up", new_user_registration_path %> |
<%= link_to "Login", new_user_session_path %> |
<% end %>
</p>
</ul>
</div>
</div>
'''
sorry for missing route.rb, here it is:
'route.rb':
Rails.application.routes.draw do
devise_for :users
#make sure devise/session/new, which means the login page is the root page
devise_scope :user do
authenticated :user do
root 'country#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
get '/country', to: 'country#index'
get '/maison', to: 'maison#index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
#root 'page#welcome'
resources :country
resources :maison
resources :introduction
resources :dashboard
end
So it looks like you have an href <li>Maison</li> that is pointing to /maison which means somewhere in your routes.rb there needs to be a route defined that matches that, as well as a controller that the route interfaces with:
get '/maison', to: 'home#index`
In the above route, we're saying "any get request to the /maison address should trigger the index method in the home_controller file in Rails.
Once you set the route and controller correctly, this should work properly.
There are a couple things:
The file should be titled routes.rb (plural)
So change route.rb to routes.rb.
Use the command $rails routes or $rake routes
To get a list of all routes you currently have available to you. If nothing shows, then rails is not recognizing your routes.
Use "Link To" for example
<%= link_to user.title, users_path(user) %>
Remove the dot from your URLs. The dot makes it a relative path, so wherever you are in your app, it will use that path as the starting point for those links. If you remove the period, it will always use your root path to build the links.
<li>Introduction</li>
<li>Country</li>
<li>Maison</li>

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?

link_to problem

I am new to Rails and I'm I am trying to create a basic blog application, but I'm having trouble linking.
I have three controllers (authors, pages and static_pages). They all work fine but I'm trying to link to two pages in static_pages. They are about.html.erb and help.html.erb. I'm using a partial to create a navigation menu at the top.
I'm getting the following error:
ActionController::RoutingError in Pages#index
No route matches {:controller=>"pages", :action=>"about"}
1: <%= link_to 'Homepage', pages_path %> |
2: <%= link_to 'List of Authors', authors_path %> |
3: <%= link_to 'About', :action => 'about' %> |
4: <%= link_to 'Help', :action => 'help' %>
The code in my menu partial is:
<%= link_to 'Homepage', pages_path %> |
<%= link_to 'List of Authors', authors_path %> |
<%= link_to 'About', :action => 'about' %> |
<%= link_to 'Help', :action => 'help' %>
My static_pages controller looks like this:
class StaticPagesController < ApplicationController
def About
end
def Help
end
end
I understand that it's probably something very simple but as I say I'm new to Rails and web development in general so any advice will be appreciated.
Trivial, make your action methods lowercase. Ruby is case sensitive with variable and method names.
class StaticPagesController < ApplicationController
def about
end
def help
end
end
Have you checked your routes using rake routes in your terminal/console? If not this is a good way to solve problems related to route errors.
Make sure that your link_to methods are trying to access the StaticPagesController, your error seems to indicate that Rails is trying to create the URL for the about action through PagesController.
Check the docs for ActionView::Helpers::UrlHelper, especially the link_to and url_for methods.

Resources