I'm trying to understand an app in which it creates a link via link_to to new_user_session_path. In my controller, there is no new nor session. The generate link is users/sign_in which you can see here : [ListenUp][1]. Also, sign_in is not in the controller. My hunch is that it is some RoR magic.
The controller :
class UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.find_by_permalink(params[:id])
#songs = Song.where(user: #user)
#friendships = #user.all_friendships
end
end
The routes
Rails.application.routes.draw do
devise_for :users
resources :users
resources :friendships
root 'pages#search'
resources :pages
resources :playlists
resources :songs
get 'search' => 'pages#search'
get 'search_results' => 'pages#search_results'
end
Part of the view that I'm trying to figure out :
<li><%= link_to "sign in", new_user_session_path %></li>
<li><%= link_to "sign up", new_user_registration_path %></li>
Thanks
[1]: http://listenup-songshare.herokuapp.com/
Rails will automatically generate helpers for you based on your route names. See Rails Routing From the Outside In for this straight from the horses's mouth.
By Convention these helpers look something like ACTION NAME + CONTROLLER NAME + "path" (or "url").
Given this routes file, you might have a new_song_path generated for you.
In addition to this, gems you add to your Gemfile can also create additional routes. Here you see that new_user_session_path is not your code.
To list all the routes in your app, including those added by other gems, run rake routes. Usually I run rake routes > tmp/routes.txt, which saves the output to a file named tmp/routes.txt (if you're not hip to bash), and I refer to this file often when developing my Rails app.
Related
so I'm new to rails and currently going through a tutorial for it on tutorialspoint. What I've gotten so far is a controller and a corresponding view for it which is an erb file. This is the code for the view:
<% if #books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id = "books">
<% #books.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Book", {:action => 'new' }%></p>
However, when I try to view this through localhost:3000 while having the
rails server
command running a WEBrick server in the background on localhost:3000,
it keeps directing me to the default view on my browser,which is being rendered by the server from the following path:
/Users/hassanali/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/templates/rails/welcome/index.html.erb
instead of the actual view within my rails application folder's view folder..which is
/Users/hassanali/Desktop/library/app/views/book/list.html.erb
I've been trying to fix this for ages now and no avail. Does anyone know what I can do?
Because you not define root_path for your app.
In config/routes.rb define root_path for it, ex:
root 'book#list'
You can tell Rails what you want '/' to route to by using root controller#action.
For example say you had a controller that looked like this.
class BooksController < ActionController::Base
def index
#books = Books.all
end
def show
#book = Books.find(params[:id])
end
end
If you wanted '/' to route to your index method, you would do the following in config/routes.rb.
Rails.application.routes.draw do
root 'books#index'
# Other routes here...
end
As a side note, if you add two root method calls, it will use the last one in the routes file.
http://guides.rubyonrails.org/routing.html#using-root
Since you have show and new action referenced you'll also likely want those routes as well. It's recommended to use RESTful routing schemes. Rails provides a useful method called resources which creates all the routes and helper methods needed to CRUD a resource.
That might look like the following.
Rails.application.routes.draw do
root 'books#index'
# A books resource
resources :books
end
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
I encourage you to read the Rails Guide on routing so you understand your options.
I create URL for sign out from system:
<li><%= link_to("Выйти", "http://bookworm.az:3000/logout") %></li>
Sessions Controllers Destroy action:
def destroy
session[:username] = nil
redirect_to "http://bookworm.az:3000/login"
end
And my routes are:
get '/logout' => 'sessions#destroy'
But when I click on link it redirects me to the
http://bookworm.az:3000/logout
Not to
http://bookworm.az:3000/login
What is the problem?
Your code is breaking Rails conventions. You should use this one instead:
view.rb
link_to "Delete", session_path(current_session), method: :delete
You should not change routes.rb resources. Keep it simple:
routes.rb
resources :sessions
Also, you should use URL helper that Rails generates from your routes.rb file, instead of using an absolute path (unless there is no other way).
I am new to rails development.
I have created a new database model called review and a controller called reviews_controller. In my routes file I have created the resource for reviews. However when I try and use the reviews#show in a link I get the error. No route matches {:action=>"show", :controller=>"reviews"}.
I have looked this error up but any of the results that I have found are more involved than the code that I am using. Is there something very basic that I am missing here.
Code for the model is
class Review < ActiveRecord::Base
belongs_to :user
end
Code for the controller is
class ReviewsController < ApplicationController
def show
#review = Reviews.find(params[:id])
end
def new
end
end
My link to in my header partial is
<li><%= link_to "Review", review_path %></li>
Code from my routes file
resources :users #New Line
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :reviews
Any help on this would be greatly appreciated as I cant get anywhere on this.
Welcome to SO!
Since you are linking to a specific review, you need to pass in the review's id to the link_to method.
<li><%= link_to "Review", review_path(#review) %></li>
Rails is smart enough to take the whole #review object to figure out its id.
Alternatively, you can link to all your reviews with
<li><%= link_to "Reviews", reviews_path %></li>
Since you said you have this link in your header partial, this might be what you actually want to do.
I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.
I have created model, view and controller:
$ rails generate scaffold Post name:string title:string content:text
I run server and I see list of posts if I open in the browser http:\localhost:3000\posts.
Now I need to create link to this page. Something like:
<%= link_to("settings", { :controller => 'groups', :action => 'index'}) %>
But I get error on opening this page:
Couldn't find Group with ID=index
How ca I create link to http:\localhost:3000\posts and which action do I use in this case?
I think the path helpers are excellent in these cases. You could do it like this:
<%= link_to("Posts", posts_path) %>
posts_path in this case will link to http://localhost:3000/posts
When you use use resources :posts in your routes.rb you automatically get a few path helpers. For example:
posts_path # /posts
post_path(#post) # /posts/1
edit_post_path(#post) # /posts/1/edit
new_post_path # /posts/new
If you have a route such as:
resources :groups
In config/routes.rb then you will have the helper groups_path. You can use rake routes to see all of your routes and helpers, but in this case you will have:
groups_path
group_path(#group)
edit_group_path(#group)
Polymorphic Routes Documentation