How can I correctly link to a static page? - ruby-on-rails

I've followed a railscast on creating and displaying static pages using a pages model and my code looks like this:
Pages model
has fields of name, permalink and description.
Routes:
get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :pages
match ':permalink', :controller => 'pages', :action => 'show'
_footer.html.erb
<div class="footer">
<div class="footer_container">
<%= link_to 'About Us', root_path('about') %>
</div>
</div>
going to /localhost:3000/about displays the about page correctly but the link in the footer wants to direct to /localhost:3000/.about and actually links to the sign up a new user page.
How can I get my link to direct to /about and display the page?
Thanks for any help its much appreciated!

root_path will always take you to users#new because that is what you specified in your routes.rb file. What you can do is name your last route with the :as key, like this:
match ':permalink', :controller => 'pages', :action => 'show', :as => 'my_page'
Then in your views, you should be able to do something like this:
<%= link_to 'About Us', my_page_path('about') %>

Related

Routing in Rails forms

I've used a form successfully in embedded Ruby that looks like this:
<%= form_for :comment, url: user_comments_path do |c| %>
<%= c.text_field :location %>
<%= c.text_field :title %>
<%= c.text_field :body %>
<%= c.submit ("Submit") %>
<% end %>
However when I tried to put this into Bootstrap I came up against problems. I then tried to make a simpler HTML form like this:
<form accept-charset="UTF-8" action="user/comments" method="post">
<input id="comment_location" name="comment[location]" type="text"/>
<input id="comment_title" name="comment[title]" type="text"/>
<input id="comment_body" name="comment[body]" type="text"/>
<input name="commit" type="submit" value="Create"/>
</form>
...but I came up against the same error message: No route matches [POST] "/users/1/comments/new/user/comments". I am using nested resources, and Rails routes in the terminal tells me that the user_comments path 'POST' equates to user/user_id/comments and the Create controller action.
In my HTML form if I enter action="user/comments" then the resulting path is "/users/1/comments/new/user/comments", if I enter action="comments" the path is "/comments", and if I enter "/" the path is "/". The path that I'm trying to reach is "user/comments" but there seems to be no way there through my HTML form.
I've racked my brains but I can't understand why this might be. Any help would be much appreciated! Thanks
ps. Routes look like this:
Rails.application.routes.draw do
get 'sessions/new'
get 'welcome/index'
root 'welcome#index'
get 'user' => 'users#show'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users do
resources :comments
end
resources :sessions
end
update routes.rb use collection instead of resources to get /users/comments like path
resources :users do
collection do
get 'comments'
end
end

Contact us gem route conflict - Rails 4

I am getting this error: ActionController::UrlGenerationError in ContactUs::Contacts#new, using this gem https://github.com/jdutil/contact_us .
The error: No route matches {:controller=>"contact_us/pages", :action=>"home"}
Where the error is occurring:
.col-xs-8
%ul
%li
= link_to "Home", :controller => 'pages', :action => 'home'
%li
= link_to "About", :controller => 'pages', :action => 'about'
%li
My routes:
devise_for :users
resources :available_times
root :to => "pages#home"
get 'about' => 'pages#about'
get 'pricing' => 'pages#pricing'
get 'users/my-bookings' => 'available_times#index'
get 'users/x34' => 'available_times#create'
get 'users/test_func/:id/:time' => 'available_times#test_func'
Gem routes
Rails.application.routes.draw do
resources :contacts,
:controller => 'contact_us/contacts',
:only => [:new, :create]
get '/contact-us' => 'contact_us/contacts#new', :as => :contact_us
end
Looks like you have a scoping issue. The view is rendered in the contact_us scope and calling a controller in that scope will have it look for it in contact_us/ rather than the entire app. I'm no big fan of using the :controller => 'pages', :action => 'home' for routes generation directly, try and use the path helper instead. It will probably work better.
= link_to "Home", root_path
= link_to "About", about_path
In routes:
get 'about' => 'pages#about', as: :about

How to custom edit_user_path with nice URL

I'm adding a nav menu in application.html.erb which navigate users in authlogic. All routers like: :logout, :register and :login seems working with custom paths.
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#new', :as => :register
Here is nav menu:
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(current_user.id)%> <%=h current_user.firstname %>
<%= link_to "Logout", :logout %>
<% else %>
<%= link_to "Register", :register %> |
<%= link_to "Login", :login %>
<% end %>
But edit_user_path is transferring me to /users/:id/edit. How do I make the nice URL for this path. I would like to make it /account/edit. It also need to disable this path /users/:id/edit to prevent user from requesting another user ID which doesnt belong to him/her. It should throw a 404 page is perfect.
Current paths in the nav menu:
Logout: /account/logout
Login: /account/login
Register: /register
I would love to have another path for edit_user_path:
Edit profile: /account/edit
Is there any way I can simply use edit_user_path(current_user.id) and the path automatically transfers me to /account/edit and disable id request.
Any help would be much appreciated! Thanks!
[Updated] This is my /config/router.rb
Appcatous::Application.routes.draw do
resources :users, :user_sessions
match "account" => "users#show", :as => :account
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
end
users.rb model is very simple:
class User < ActiveRecord::Base
acts_as_authentic
validates :firstname, :presence => true
validates :lastname, :presence => true
end
It would be helpful if you included all the routes in question - which means also the routes for the users. Did you create those with:
resources :users
In case you did, you'll have to disable that command and recreate just the routes you need manually with (example!):
match 'account/edit' => 'users#edit', :as => :edit_user
or whatever your controller is named. That way you'll get your "/account/edit" - however, you'll also have no way to transfer the user id that way. That one you'll have to transfer by other means, like through a variable stored in a cookie or a session.
The latter method is the usual way of remembering user authentification anyway.

ROR link_to method problem

I'm trying to pass some values through a link and I want them to be invisible. These are the options I've tried:
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
<%= link_to 'Add comment',{ :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, %>
<%= link_to 'Add comment', new_comment_path, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
First option - treats method as a parameter:
http://localhost:2000/comments/new?idea_id=1&method=post&user_id=1
Second option - goes like this: http://localhost:2000/comments/new?idea_id=1&user_id=1
and also causes routing error: "Routing Error No route matches "/comments/new"
Third option - loads the form, but of course is like: http://localhost:2000/comments/new?idea_id=1&user_id=1
Fourth option - looks good (http://localhost:2000/comments/new) but the same routing error like the second one.
What am I doing wrong?
Thanks in advance.
PS
I was asked to give my routes, so here they are:
resources :rights
resources :comments
resources :ideas
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'main#home'
#match '/comments/new' => "comments#new" # this doesn't help
match '/home', :to => 'main#home'
match '/contact', :to => 'main#contact'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/signup', :to => 'users#new'
If you have RESTful routes
<%= link_to 'Add comment', new_comment_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
should be
<%= link_to 'Add comment', comments_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
As others have said, it sounds like you have a problem in your routes file. Either check if you have the resource :comments defined or post your routes file here and we'll help you out. It's possible that it's not working because you are trying to POST...
If you want 'invisible' variables (I assume you mean that you do not want the variables to appear in the URL), you'll have to POST to the page rather than just link to it. In this case, your second example is the best bet. It goes against convention to POST to /new so that could be what is causing the 'no routes' error if you're using resource :comments
Give this a try in your routes.rb:
match '/comments/new' => "comments#new"
Give that a try, it should load the correct page and give you access to the variables you passed through to it through params.
Please note, that this goes wildly against convention. Is there a reason why you don't want those variables to appear in the URL? It's likely there's a better way of doing what you're thinking and if you explain we can advise you as best as we can.
Have you properly defined the routes?
Can you show how they are?
You should have something like this for that to work: resource :comments
Also, in general /new works with a GET, and a POST is sent when creating...
The method is part of html_options, you need to separate the two hashes like so:
<%= link_to 'Add comment', {:controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>

Ruby routes and link_to, custom :action route problem

I'm trying to get this link:
<%= link_to('Edit', :action => 'manage', :id => user) %>
even tried explicitly <%= link_to('Edit', {:controller => 'users', :action => 'manage', :id => user}, :method => :get) %>
to show the link in HTML as
'/users/manage/1' or '/users/1/manage'
but it shows up as
'/users/manage?id=1'
I can see in my routes:
manage_users GET /users/manage(.:format) {:action=>"manage", :controller=>"users"}
...
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
so I added a map.connect, but it was added to users
users GET /users/manage/:id(.:format) {:action=>"manage", :controller=>"users"}
but without any success. The link is still '/users/manage?id=1'
This doesn't work anymore than the above.
GET /users/:id/manage(.:format) {:action=>"manage", :controller=>"users"}
Now, when I put the :action in link_to, to 'edit' i.e.
<%= link_to('Edit', :action => 'edit', :id => user) %>
The routes.rb edit_user GET /users/:id/edit/(.:format) works, with a link showing up of
'/users/1/edit'
How do I get my link_to to show the same link when it is 'manage' instead of 'edit', i.e. showing a link of 'users/1/manage' instead of '/users/manage?id=1'??? Is it because my above map.connect is being added to users, when it should be added to 'manage_users'?
Thank for the help. I'll be trying to figure it out.
Peace.
BTW, /users/manage?id=1 works, I just want the proper rewrite link to click on.
EDIT routes.rb
map.resources :users, :collection => {:manage => :get}
#map.manage_user '/users/:id/manage', :controller => :users, :action => :manage
#map.resources :users, :member => { :manage => :get }
#map.connect 'users/manage/:id(.:format)', :controller => 'users', :action => 'manage', :conditions => { :method => :get }
map.resources :categories
map.resources :posts
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
so I added a map.connect, but it was added to users
I suspect you added map.connect after other definitions, which would give it lowest priority. Try putting it in the beginning of routes.rb file.
You can also use named routes to avoid confusion:
map.manage_user '/users/:id/manage', :controller => :users, :action => :manage
and then refer it as
link_to 'Manage', manage_user_path(:id => user)
edit
If that doesn't work, please show your routes.rb file.
You should change collection to member in your routes.rb when defining map.resources :users. Then you'll get nice /users/1/manage link.
Also, when creating a link try this:
<%= link_to 'Manage', manage_user_path(user) %>

Resources