I need some help in the rails routing - ruby-on-rails

Hey guys this is my routes file
Rails.application.routes.draw do
get 'home/store'
get 'home/chat'
get 'home/index'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
and this is my controller
class HomeController < ApplicationController
def index
end
def chat
end
def store
end
end
and i am linking my three pages index.html.erb, chat.html.erb, store.html.erb which are in the home folder
like this in application.html.erb
<ul class="nav navbar-nav">
<li><link_to "Home","index.html></li>
<li><link_to "Buy Games","store.html"%></li>
<li><link_to "Watch Videos","#"%></li>
<li><link_to "Ask The Experts","chat.html"%></li>
</ul>
now the problem is that my root which is localhost:3000 is working fine but when i open any other link like chat through application.html.erb then the address bar is something like this "localhost:3000/chat.html.erb" but my location of file is in view/home/chat.html.erb same thing happens when i open any other link
so what should i do to make the routing work also i have tried putting home\chat.html in the link tag in apllication.html.erb it works but when i go back it adds another \home in the address bar
also my background images which i put in the inline style are displaying but when i open any other webpage like chat.html.erb then the inline css background images are not uploading

You need to
config/routes.rb
Rails.application.routes.draw do
get '/home/store', to: "home#store", as: :store
get '/home/chat', to: "home#chat", as: :chat
get '/home/index', to: "home#index", as: :index
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
your application.html.erb
<ul class="nav navbar-nav">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Buy Games",store_path %></li>
<li><%= link_to "Ask The Experts", chat_path %></li>
</ul>

First of all, be sure to look over stack overflow's markdown guide to help you with styling your post in order to make it readable. Also, welcome to Stack Overflow :)
Okay, so it looks like you don't quite have the right syntax for specifying your links using rails' link_to helper with erb. The correct syntax is:
<%= link_to "my link", your_route %>
But it looks like you typed:
<link_to "Home","index.html>
You missed a closing " and didn't properly open or close the erb tags. So the correct syntax would be:
<%= link_to "Home", "index.html" %>
Then finally for all your links:
<li><%= link_to "Home", "index.html" %></li>
<li><%= link_to "Buy Games", "store.html" %></li>
<li><%= link_to "Watch Videos","#" %></li>
<li><%= link_to "Ask The Experts","chat.html" %></li>
be sure to look at the rails guides regarding the link_to helper for more information.
Hope that helps and good luck!

Related

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>

Seeking a better way to define my Rails routes/paths

I have 2 main components to my application, Users and Properties. The URL should be structured like: hostname.com/users/:user_id/properties/:property_id. I believe I've made a configuration error somewhere, because Rails never recognizes "property_path" or any of its variants, and I've had to hard code them in to get the redirects to work.
routes.rb
resources :users do
resources :properties
end
users/show.html.erb - Notice I had to hard code the path, instead of simply linking to "i"
<% #user.properties.each do |i| %>
<li><%= link_to "#{i.address}", "/users/#{#user.id}/properties/#{i.id}" %></li>
<% end %>
How can I better define my routes so that I can link above to just "i", which would represent the "properties_path", and would auto redirect to that show page?
You don't have to hardcode it. You can do:
<% #user.properties.each do |property| %>
<li><%= link_to property.address, [#user, property] %></li>
<% end %>
Yes, it's that simple. For more information, you can go to Rails guides.

access rails url helper from deface

I am including Spree to an existing site. I am changing the spree header with Deface to render my site's header instead. Therefore I use the following Deface DSL code
<!-- replace_contents "header[data-hook]" -->
<%= render :partial => "layouts/my_site_header.html.erb" %>
And inside _my_site_header.html.erb I have something like this
<ul>
<li><%= link_to "Home", home_path %></li>
<li><%= link_to "Game", game_path %></li>
<li><%= link_to "Community", community_path %></li>
</ul>
Which gives me the following error
undefined local variable or method `home_path' for #<#<Class:0x8a73c20>:0x8af0e58>
I understood that the code get executed by Deface in the Spree scope, thus my site's url helpers are undefined. I could solve this using the full method name like Rails.application.routes.url_helpers.home_path
However, I don't really feel like adding this for all of my links. Isn't there a way to tell Spree to include the url helpers of my site? Please help!
There is a shorter version which you can use from Rails::Engine called main_app.
<ul>
<li><%= link_to "Home", main_app.home_path %></li>
<li><%= link_to "Game", main_app.game_path %></li>
<li><%= link_to "Community", main_app.community_path %></li>
</ul>
I would highly recommend using this to avoid conflicts between your application and Spree (such as your app home vs. Spree's home).

Can Ruby on Rails Automatically generate links to articles?

so my question is after i generate a scaffold and i have the ability to post articles to my site it usually is a link like www.mysite.com/articles/1
my question is...is it possible to have your application automatically generate a link to your www.mysite.com/articles/1 page
because right now i have to manually go into the HTML and add the link
=link_to 'my article', /articles/1
i was just wondering if its possible to make the application automatically generate the link for you?
Yes, it's possible. I don't know based on your question to what articles you would like to link, but if you have this set up using resources in your routes, this should work:
<ul>
<% Article.all do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
Or if you're using HAML (as it appears you are based on the format of your question, but you don't specify:
%ul
- Article.all do |article|
%li= link_to article.title, article
You could modify the query to limit results, paginate them, sort them, etc. as you desire.

Rails 3.1 routing confusion

I am trying to create a link to a record in my application:
<ul class="presentation-box">
<% #presentations.each do |presentation| %>
<li><%= link_to "Presentation", presentations_path(#presentation) %></li>
<li><%= presentation.author %></li>
<% end %>
</ul>
With the following line in the routes file:
resources :presentations
root :to => 'presentations#index'
For some reason, when I click the link it's taking me to the Presentation index view. I believe it should be taking me to the show view of the individual record?
Am I missing something obvious?
Your link_to is incorrect.
presentations_path will actually point you to the index, you want presentation_path(presentation) to point directly to the resource.
Also, you can just do <%= link_to 'Presentation', presentation %> and Rails will build the correct path for you
Change it to presentation_path(presentation)

Resources