ActiveRecord::RecordNotFound in PostsController#show clicking a link - ruby-on-rails

<li><%= link_to('More Commented', posts_morecommented_path) %></li>
Error
ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Post with id=morecommented
Request
Parameters:
{"id"=>"morecommented"}
Where am I doing the mistake?
postscontroller#show action
def show    #post = Post.find(params[:id])    ...   end
morecommented.html.erb
<% #moreCommented.each do |t| %>
<%= link_to t.title, :controller => '/posts', :action => 'show', :id => t.id %><br/>
<% end %>
rake routes
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
....
posts_morecommented /posts/morecommented(.:format) {:controller=>"posts", :action=>"morecommented"}
routes.rb:
resources :posts
match "posts/:id/categ" => "posts#categ"
match "posts/:id/tag_posts" => "posts#tag_posts"
match "posts/searcharchive" => "posts#searcharchive"
match "posts/morecommented" => "posts#morecommented"

move the match before the resources call
match "posts/morecommented" => "posts#morecommented"
resources :posts
Alternatively you can do
resources :posts do
get :morecommented, on: :collection
end

Your problem is inside your routes.rb file since routes are matched from the top to the bottom action posts/morecommented matches posts/:id action with params[:id] equal to morecommented One solution as Gerry mentioned is to change order and move the match "posts/morecommented" => "posts#morecommented" before the call resources :posts in your routes.rb file the other one is to set requirements on :id in your posts/:id route.

Related

Routing Error - Custom Contoller Method (Adding Custom routes in resource routes)

I want to add link to friends list but i get routing error
No route matches {:action=>"friend_list", :controller=>"users_controller"}
users_controller
def friend_list
#frnds = User.find_friends(current_user)
end
routes
devise_for :users
resources :users do
member do
get :follow
get :unfollow
get :show
end
match 'users/:id/friend_list' => 'users#friend_list', via: [:get]
link
<li><%= link_to "Friends", :controller => :users_controller, :action => :friend_list%></li>
end
match 'users/:id/friend_list' => 'users#friend_list', via: [:get]
root 'home#front'
instead of member block use collection block like this
resources :users do
collection do
get "follow"
get "unfollow"
get "show"
end
end
member block will append :id in routing while collection block will allow you add custom routes in resource routes
in link_to use this
<%=link_to "Friends", controller: "users", action: "friend_list"%>

Why Rails dont recognize route

I have in routes.rb
get '/cities/:city/:section' => 'cities#show', as: 'city_section_slug'
get '/cities/:city/:section/:subsection' => 'cities#show', as: 'city_subsection_slug', :constraints => { :subsection => /[^\/]*/ }
And rake show these routes
city_section_slug GET (/:locale)/cities/:city_id/:section(.:format) cities#show {:locale=>/ru|en/}
city_subsection_slug GET (/:locale)/cities/:city_id/:section/:subsection(.:format) cities#show {:subsection=>/[^\/]*/, :locale=>/ru|en/}
But when I try create a link:
= link_to city_subsection_slug_path(#city,section.alias, subsection.alias)
Iv got such error:
ActionController::RoutingError (No route matches {:controller=>"cities", :action=>"show", :locale=>:ru, :city=>#<City id: 42, name: "City">, :section=>"events", :subsection=>"sobytiya/ya-ochevidets"}):
Any ideas where I am wrong?
The reason is your constraint on subsection param. You specified that it can be anything as long as it does not contain / character. Your subsection does conatin it hence the error. If you want to allow / character, then do:
get '/cities/:city/:section/:subsection' => 'cities#show',
as: 'city_subsection_slug',
constraints: { :subsection => /^[a-z0-9_\/]+$/ }
Eventually, you can use glob param:
get '/cities/:city/:section/*subsection' => 'cities#show',
as: 'city_subsection_slug',
#config/routes.rb
resources :cities, only: [] do
get ":section", action: :show, as: :city_section_slug #-> url.com/cities/:city_id/:section
get ":section/*subsection", action: :show, as: :city_subsection_slug #-> url.com/cities/:city_id/:section/:subsection
end
The corresponding links:
<%= link_to "x", city_section_slug_path(#city, section.alias) %>
<%= link_to "x", city_subsection_slug_path(#city, section.alias, subsection.alias) %>
Wildcard
If you're expecting to send sobytiya/ya-ochevidets to your subsection path, you'll be best using a wildcard route

Can't link to another page with user

I want link to another page with my user, but i have an error:
RuntimeError in WelcomeController#edit
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
In my current page
<%= link_to "edit", welcome_edit_path(#user) %>
thi is my controller
def edit
user = User.find_by_id(#user.id)
end
in my page i want link to
<%= user.id %>
I know my problem is my #user was nil, but i dont know how to link to another page with my #user
So, please! help me
this my routes file
get "micropost/new"
get "user/new"
get "user/saved"
get "post/new"
get "post/show"
get "welcome/index"
get "welcome/sucess"
get "welcome/edit"
root :to => "welcome#index"
get '/users/:id', :to => 'welcome#sucess', :as => "user"
match '/relations', to: 'relation#create', via: 'post'
match '/relations/:id', to: 'relation#destroy', via: 'delete'
resources :users
resources :relations, only: [:create, :destroy]
resources :microposts
match '/login', to: 'welcome#create', via: 'post'
match '/logout' => 'welcome#destroy', as: :logout
match '/create', to: 'micropost#create', via: 'post'
match '/signup', to: 'user#signup', via: 'post'
Your code in edit method makes no sense. First thing, you have to define your route properly:
get '/welcome/edit/:id', to: 'welcome#edit', as: 'welcome_edit'
Then, in the WelcomeController:
def edit
#user = User.find(params[:id])
end
and the link to this page:
<%= link_to 'edit', welcome_edit_path(#user) %>

URL helper for acts-as-taggable-on

The following code isn't working
<p>Tags: <%= #video.tags.map { |t| link_to t.name, tag_path(t)}.join(', ') %></p>
Rails complains about undefined method 'tag_path' for #<#<Class:0x007fd0f0260890>:0x007fd0ebce5f68>
I think tag_path is just not the right url_helper, can anybody explain which one to use here?
My rake routes
video_tags GET /videos/:video_id/tags(.:format) tags#index
POST /videos/:video_id/tags(.:format) tags#create
new_video_tag GET /videos/:video_id/tags/new(.:format) tags#new
edit_video_tag GET /videos/:video_id/tags/:id/edit(.:format) tags#edit
video_tag GET /videos/:video_id/tags/:id(.:format) tags#show
PATCH /videos/:video_id/tags/:id(.:format) tags#update
PUT /videos/:video_id/tags/:id(.:format) tags#update
DELETE /videos/:video_id/tags/:id(.:format) tags#destroy
videos GET /videos(.:format) videos#index
POST /videos(.:format) videos#create
new_video GET /videos/new(.:format) videos#new
edit_video GET /videos/:id/edit(.:format) videos#edit
video GET /videos/:id(.:format) videos#show
PATCH /videos/:id(.:format) videos#update
PUT /videos/:id(.:format) videos#update
DELETE /videos/:id(.:format) videos#destroy
Apart from that, I did not change my routes.rb, which looks like this:
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
root 'welcome#index'
resources :articles do
resources :comments
end
resources :videos do
resources :tags
end
end
Alright, I got it working this way:
Added resource to routes.rb:
resources :videos do
resources :tags
end
With the routes set up, I changed my RHTML to look like this:
<p>
Tags:
<%= raw #video.tags.map {
|t| link_to t.name, video_tag_path(:video_id => #video.id, :id => t.id)
}.join(', ') %>
</p>
Had to specify the video_id and tag id parameter explicitly.
For tags, you have to generate a controller afterwards.

What is wrong with my routes?

root :to => "index#home"
#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get
match "/tattoo" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get
members section and its nested images
resources :members, :except => [:new, :create] do
resources :tattoos
end
Thats whats in my routes.rb file. They produce:
root /(.:format) {:controller=>"index", :action=>"home"}
submit GET /submit(.:format) {:controller=>"index", :action=>"new"}
tattoo POST /tattoo(.:format) {:controller=>"index", :action=>"create"}
GET /tattoo/:id(.:format) {:controller=>"index", :action=>"show"}
tagged GET /tagged(.:format) {:controller=>"index", :action=>"tagged"}
DELETE /tattoo/:id(.:format) {:controller=>"index", :action=>"destroy"}
tattoos GET /tattoos(.:format) {:controller=>"index", :action=>"index"}
members GET /members(.:format) {:action=>"index", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET /members/:id(.:format) {:action=>"show", :controller=>"members"}
PUT /members/:id(.:format) {:action=>"update", :controller=>"members"}
DELETE /members/:id(.:format) {:action=>"destroy", :controller=>"members"}
But i have a problem. For some reason, when I try to go to mysite.com/submit
I used to get this error
No route matches {:controller=>"images"}
on
<%= form_for #tattoo, :html =>{:multipart => true} do |f| %>
but that has magically changed to:
undefined method `images_path'
on the same line.
when my controller has this:
indexcontroller
def new
#tattoo = Image.new
end
def create
#tattoo = Image.new(params[:image])
if #tattoo.save
flash[:success] = "Tattoo sent in for approval!"
redirect_to(images_path)
else
render :action => "new"
end
end
And then this link_to:
<%= link_to "Manage tattoos", member_tattoos_path() %>
give me this error:
No route matches {:controller=>"tattoos"}
I thought I was beginning to understand routes and had a decent grasp but I dont get whats going on!
You need to pass in a member object to edit_member_path.
<%= link_to "Edit profile", edit_member_path(#member) %>
edit_member_path should know the id of the member you want to edit. Please try
<%= link_to "Edit profile", edit_member_path(#member) %>
For No route matches {:controller=>"images"}; since the action image is not defined in your route, please try to stop and restart the server and check if there is any plugin like Paperclip in place.

Resources