Rails Routing (root :to = ...)
Had this same problem and this worked for me:
root :to => "pages#show", :id => '1'
Rails route: define root to namespace
namespace :mobile do
root :to => "posts#index"
resources :posts
end
root :to => "posts#index"
resources :posts
instead of
root :to => "posts#index"
resources :posts
namespace :mobile do
root :to => "posts#index"
resources :posts
end
Setting parameters on the root route in Ruby on Rails
All users are authenticated
So what you're asking is how to scope the "homepage" @posts
to those of the current_user
.
There are two ways to do it (Controller
and Devise
):
Controller
#config/routes.rb
root "posts#index" #-> url.com
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = user_signed_in? ? current_user.posts : Post.all
end
end
#app/views/posts/index.html.erb
<% @posts.each do |post| %>
<%= post.title %>
<% end %>
This is the simplest way to pull it off.
The routes don't matter -- if your user is signed in, you can scope the data around their current_user
object.
In both cases, you wish to access url.com
, so you'd match this with routing that to the posts#index
. The @posts
variable can then be scoped to whichever setup works best for you.
Devise
#config/routes.rb
authenticate :user do
root "posts#user", as: :authenticated_root #-> url.com
end
unauthenticated :user do
root 'posts#index', as: :root #-> url.com
end
Devise has the authenticated
&& unauthenticated
route helpers to give you different sets of routes, depending on whether the user is logged in.
There are 3 helpers in total:
authenticate
- if user is logged inauthenticated
- only if the user is logged in (hidden other times)unauthenticated
- if user is not logged in
You could use the above to access the following:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all
end
def user
@posts = current_user.posts
render :index
end
end
This is not much different than the above method; the reason you'd use it is if you had a marked difference in the flow. IE you set data in posts#index
which was completely different to posts#user
.
In my example, both methods use index.html.erb
; you could easily split them...
#app/views/posts/index.html.erb
<% @posts.each do |post| %>
....
<% end %>
Redirect PUT route to root_url automatically in routes.rb
I would personally handle this with your webserver software.
If you're using Apache, you could use something like:
#public/htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^*.txt$ http://example.com/ [R=301,L]
If you wanted to handle it in the Rails routes, you'd be able to use a redirect:
#config/routes.rb
unless Rails.env.development?
scope constraints: { format: 'txt' } do
match '*path', to: redirect('/'), via: [:get, :post, :put]
end
end
You'd have to make sure you are capturing only the routes you need to redirect, though.
The problem you have with your setup is that it will capture every route you send to your app, and redirect to root
. In fact, this may end up causing an infinite recursion error.
My code above works only in production / staging (as yours).
It takes any .txt
route passed through the GET
, POST
or PUT
HTTP verbs, redirecting to the root path.
setting an app's root to a route in an engine
I explicitly declared the controller I was using without the name spacing in config/routes.rb
.
PagesController = MyEngine::PagesController
root action: :show, controller: :pages
That did it. I'm wondering if I found a bug with the module
option?
Rails routing to root
When you use root :to
rails 3 automatically creates the helper methods root_url and root_path for referencing your application root. These methods are often used in gems to reference your applications root and I'm not actually sure where these would point or if they would even work if you don't specify anything (never tried it). Plus it's the "rails way" of doing things so it's usually best to follow unless you have a really good reason.
Can't change root route rails
Why do you want the index to be show page? Well, how can you do that? The show
action requires a id
to be passed to the controller so that a particular project can be displayed to the user. How can you pass in a id
when the user is requesting for /
?
Does that make sense?
I would suggest you to leave the root to the index
page. In the index view, link each project to its show page. Its unnecessary to have a nested form in the index page.
Or if you want to list all projects and tasks in the root page, modify your index
view to loop through each project's tasks.
@projects.each do |project|
# display project's information
project.tasks.each do |task|
# display the task information
# display a new task button
end
end
But you can't display the nested form like you asked. Because the form requires @project
and @task
which you can't determine in the index action
. Maybe you can add remote: true
to the "New task" and then trigger a JS response to render the form in a modal.
If this sounds new to you, please see https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails
This will walk through you using AJAX in your rails application.
Hope this helps!
Rails: redirect all unknown routes to root_url
If your project is powered by rails 3, add simply this line to your routes.rb
match '*path' => redirect('/')
Edit: If you're on Rails 4 or 5
match '*path' => redirect('/'), via: :get
or
get '*path' => redirect('/')
Related Topics
"Ago" Date/Time Functions in Ruby/Rails
Rails: Validating Min and Max Length of a String But Allowing It to Be Blank
Ruby Strftime: Month Without Leading Zero
Rails - Finding Intersections Between Multiple Arrays
Ruby - Adding a Directory to $Load_Path - What Does It Do
How to Mix a Module into an Rspec Context
How to Check If a Folder Exists in Chef
Getting Memory Usage of My Process from Osx Using Ruby
Package Ruby to a Standalone MAC Os X Application
Rails - How to Create a Model Linked to Two of Another Model
Detecting Image Equality at Different Resolutions
Generate Ruby Classes from Xsd
Ruby: Cannot Parse Excel File Exported as CSV in Os X
How to Open a File and Search for a Word