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)
Related
In this Rails app, Users write Stories. A Story may be part of a Collection. Collections belong to the User who created it.
I am trying to show a single Story with links to the Collections it is part of. The collection.name part works but I can't get the collection_path right. Thanks for your help.
stories/show.html.erb
<% #story.collections.each do |collection| %>
<%= link_to collection.name, collection_path %>
<% end %>
rake routes for collections
user_collections GET /users/:user_id/collections(.:format) collections#index
POST /users/:user_id/collections(.:format) collections#create
new_user_collection GET /users/:user_id/collections/new(.:format) collections#new
edit_user_collection GET /users/:user_id/collections/:id/edit(.:format) collections#edit
user_collection GET /users/:user_id/collections/:id(.:format) collections#show
routes.rb
resources :users do
resources :collections
Solved it by using the following with the help of Sebastián Palma who answered this earlier.
<% #story.collections.each do |collection| %>
<%= link_to collection.name, user_collection_path(collection.user, collection), class: 'btn btn-lake' %>
<% end %>
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!
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.
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).
coordinates GET /coordinates(.:format) coordinates#index
POST /coordinates(.:format) coordinates#create
new_coordinate GET /coordinates/new(.:format) coordinates#new
edit_coordinate GET /coordinates/:id/edit(.:format) coordinates#edit
coordinate GET /coordinates/:id(.:format) coordinates#show
PUT /coordinates/:id(.:format) coordinates#update
DELETE /coordinates/:id(.:format) coordinates#destroy
tweets_search GET /tweets/search(.:format) tweets#search
tweets_index GET /tweets/index(.:format) tweets#index
class TweetsController<ApplicationController
def index
#include 'coordinates_controller.rb'
include SearchHelper
include ParamasHelper
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
In my Rails app, I have two tables named Coordinates and Tweets. I have four actions to be rendered.
My routes.rb file
Tweetsblog2::Application.routes.draw do
resources :tweets, :coordinates
get "tweets/show"
get "tweets/index"
match "/tweets/show" => "tweets#show"
match "/tweets/index" => "tweets#index"
Whenever I navigate to http://localhost:3000/tweets, it's showing tweets/index instead of tweets/show and the same error I am getting with different names.
When I navigate to http://localhost:3000/tweets/show, it's giving ArgumentError in TweetsController#show.
When I navigate to http://localhost:3000/tweets/index its giving ArgumentError in TweetsController#show same thing
My code for show.html.erb:
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= text_field_tag(:search) %>
<%= submit_tag("get results ") %>
<% end %>
My code for index.html.erb:
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
It's not routing to the proper page. Please help me, I am stuck with this.
you have to write
resources :tweets, except: [:index,:show]
because you declared you resource first, so rails is trying to match its default routing instead of your custom action:
get "tweets/index"
-updating per comment-
Tweetsblog2::Application.routes.draw do
resources :coordinates
get "tweets/show" => "tweets#show"
get "tweets/index" => "tweets#index"
Removing the resources :tweets should fix your issue and allow you to use just call the 2 options. The initial resources :tweets tells rails that you wanted resourceful routes (index shows all of the resource, show a specify one, etc..). So just building out the 2 non-resourceful routes as above sounds like what you want. The way I normally do something like this is to just include the search form in in the index page and if there are no search params then show all tweets.)