I am new to Rails and I'm I am trying to create a basic blog application, but I'm having trouble linking.
I have three controllers (authors, pages and static_pages). They all work fine but I'm trying to link to two pages in static_pages. They are about.html.erb and help.html.erb. I'm using a partial to create a navigation menu at the top.
I'm getting the following error:
ActionController::RoutingError in Pages#index
No route matches {:controller=>"pages", :action=>"about"}
1: <%= link_to 'Homepage', pages_path %> |
2: <%= link_to 'List of Authors', authors_path %> |
3: <%= link_to 'About', :action => 'about' %> |
4: <%= link_to 'Help', :action => 'help' %>
The code in my menu partial is:
<%= link_to 'Homepage', pages_path %> |
<%= link_to 'List of Authors', authors_path %> |
<%= link_to 'About', :action => 'about' %> |
<%= link_to 'Help', :action => 'help' %>
My static_pages controller looks like this:
class StaticPagesController < ApplicationController
def About
end
def Help
end
end
I understand that it's probably something very simple but as I say I'm new to Rails and web development in general so any advice will be appreciated.
Trivial, make your action methods lowercase. Ruby is case sensitive with variable and method names.
class StaticPagesController < ApplicationController
def about
end
def help
end
end
Have you checked your routes using rake routes in your terminal/console? If not this is a good way to solve problems related to route errors.
Make sure that your link_to methods are trying to access the StaticPagesController, your error seems to indicate that Rails is trying to create the URL for the about action through PagesController.
Check the docs for ActionView::Helpers::UrlHelper, especially the link_to and url_for methods.
Related
I am new to RoR and still don't have enough experience on solving the different errors that may appear to me. In this case I am designing a blog where I can post articles. More specifically, my problem is related to deleting these articles.
As far as I know, writing:
resources :articles
in the routes file is an alternative for writing:
get "/articles" #index
post "/articles" #create
delete "/articles/:id" #delete
get "/articles/:id" #show
get "/articles/new" #new
get "/articles/:id/edit" #edit
patch "/articles/:id" #update
put "/articles/:id" #update
When I try to delete an article I get the following error:
No route matches [POST] "/articles/1"
The code I wrote was:
View
<% #articles.each do |art| %>
<%= art.title %>
<div>
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
</div>
<% end %>
Controller
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
It sounds like you have this in your view:
<%= art.body %> - <%= link_to "Delete", art, method: :destroy %>
But you actually need:
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
I'd advise double-checking this in your app based on your reply to a comment from #GonzaloRobaina.
It looks to me like you're missing the correct path in your code. It should work with something like this :)
<%= link_to "Delete, article_path(art), method: :delete %>
In my application I have a user, whose table in the database contains a boolean column for admin status. I want an admin to be able to change the admin status of any other user. The relevant view is:
<% #users.each do |user| %>
<tr>
...
<% if current_user && (current_user.id == user.id || current_user.admin) %>
...
<% if current_user.admin %>
<td><%= link_to 'Make admin', user.id.to_s + '/make_admin', {:method => 'post', :action => 'make_admin'} %></td>
<% if user.admin %>
<td><%= link_to 'Revoke admin', user.id.to_s + '/revoke_admin', {:method => 'post', :action => 'revoke_admin'} %></td>
<% end %>
...
</tr>
<% end %>
I wasn't sure how else to do it so I cobbled together the URL by concatenating the user's ID and the specific query. My controller begins with the restrictions which seem to prevent unauthorized access to these methods:
class UsersController < ApplicationController
...
before_action :admin_logged_in, only: [:edit, :update, :destroy, :make_admin, :revoke_admin]
And finally, my routing file looks like this:
Rails.application.routes.draw do
...
post ':id/make_admin' => 'users#make_admin'
post ':id/revoke_admin' => 'users#revoke_admin'
It seems to work, but I was wondering if there was a better way to do it. I've read the routing guide on Rails but little of it makes sense to me right now. I've tried linking directly to the user with
<%= link_to 'Make admin', user, {:controller => 'users', :action => 'make_admin'} %>
But I would receive an error about no route being defined for [POST]/user_id or something along those lines. That feels like the better solution in this case; if it is, what should I do in the routes.rb file to address this?
You can create your routes as follows:
resources :users do
member do
post 'make_admin', as: :make_admin
post 'revoke_admin', as: :revoke_admin
end
end
I hope this helps you out
You can add a helper to your routes like this :
post ':id/make_admin' => 'users#make_admin', as: 'make_admin'
Now make_admin_path(user.id) will link to that. You can view all helpers either by running rake routes in your site's directory or going to localhost:3000/rails/info
As for the error you got, I can't say for certain without seeing the full error, but when you <%= link_to 'text', user %> ruby interprets user at the path, and tries to find a user_path and the {:controller => 'users', :action => 'make_admin'} is a hash of HTML attributes that get added.
I have a rails 3 app, and when I click the link to my terms page, it routes to a completely different controller, than what the routes should use. Stranger still, the route works when I'm not logged in, and I'm using devise.
I get this error when clicking the link when I'm logged in.
No route matches {:action=>"edit", :controller=>"users", :id=>nil}
<%= link_to "Terms", terms_path %>
Routes (in the order they appear in routes.rb):
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
member do
get :following, :followers
post :accept
end
end
match '/terms', to: 'static_pages#user_agreement'
Static Pages Controller
def user_agreement
end
Rake Routes
terms /terms(.:format) static_pages#user_agreement
This also happens for every other action that I've routed this way to the staticpages controller, but not for any other actions that route to different controller.
Update: Terms Page
Header:
<%= link_to "Follow", users_path %>
<%= link_to current_user.name, current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
Footer:
<%= link_to "Welcome", welcome_path %>
<%= link_to "Settings", edit_user_path(#user) %>
<%= link_to "Terms", terms_path %>
All the content is pure html.
Thanks in advance
You have a link to edit_user_path with no #user as hinted in the comments.
You should almost certainly be using current_user anyway.
Noob here.
Trying to figure out how to display a method in my controller into my index page. Here is what I have thus far.
Controller -
class SammichesController < ApplicationController
def index
#sammiches = Sammich.all
end
def create
#sammich = Sammich.find_by_name(params[:sammich][:name])
end
def random
#sammichy = Sammich.rand(params[:sammich][:name])
end
end
Routes-
Sammiches::Application.routes.draw do
resources :sammiches do
get "random"
end
root :to => 'sammiches#index'
Index-
<h1>All my Sammiches</h1>
<%= form_for Sammich.new do |f| %>
<%= f.label :sammich %>
<%= f.text_field :name %>
<%= f.submit 'Find Sammich', :id => 'sammich_submit' %>
<% end %>
<%= link_to "Random sandwich", sammich_random_path %>
routes-
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
sammiches GET /sammiches(.:format) sammiches#index
POST /sammiches(.:format) sammiches#create
new_sammich GET /sammiches/new(.:format) sammiches#new
edit_sammich GET /sammiches/:id/edit(.:format) sammiches#edit
sammich GET /sammiches/:id(.:format) sammiches#show
PUT /sammiches/:id(.:format) sammiches#update
DELETE /sammiches/:id(.:format) sammiches#destroy
root / sammiches#index
Error-
localhost:3000 - Routing Error
No route matches {:action=>"random", :controller=>"sammiches"}
Try running rake routes for more information on available routes.
Any assistance would be much appreciated.
Thanks!
If you look at your route it has :sammic_id in there as well:
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
Which means you need to pass an id to your URL helper sammich_random_path which you haven't.
Update your routes to this:
resources :sammiches do
collection do
get "random"
end
end
After adding that your route would be just /sammiches/random
I have a Post model and a Comment model (which is a nested resource of the first model):
resources :posts do
resources :comments
end
posts/show.html.erb:
<%= render #comments %>
I think I have some error in here:
comments/_comment.erb
<%= link_to "Edit Post Comment", [#post, edit_comment_path(comment)] %>
because I get this error:
undefined method `edit_comment_path' for #<#<Class:0xb439d8c>:0xaeaf4c0>
Any suggestions to fix this?
If you run rake routes you can see the route names, in your case the route name should be edit_post_comment_path rather than just edit_comment_path.
Maybe <%= link_to "Edit Post Comment", [#post, :edit, comment] %> or <%= link_to "Edit Post Comment", edit_post_comment_path(#post, comment) %> (untested, can't test here).
Because edit_comment_path is, like rails says, undefined.