I want to a searching function for users. It does not work. So I simplified the method, I just want to refresh the index page when I hit the search button. But it still does not work, it said
ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=search.
please tell me Why
my controller
class UsersController < ApplicationController
load_and_authorize_resource :except => [:index]
def search
redirect_to users_path
end
end
My view
<%= form_tag users_search_path, :method => 'get' do %>
<td><%= text_field_tag :username, params[:username] %></td>
<%= submit_tag "Search", :class => "buttons buttons-rounded buttons-flat-action", :id => "button-new"%>
<br><br><br>
<% end %>
My Route
Procedures::Application.routes.draw do
devise_for :users
#### USER MANAGEMENT ####
resources :users do
resources :rateofpays # professional timesheet
resources :roles
resources :biographies
resources :qualifications do
collection do
put 'complete', :action => 'complete'
end
end
resources :supervisors
end
#### users search ####
get 'users/search' => "users#search", as: 'users_search'
The line
get 'users/search' => "users#search", as: 'users_search'
...is too far down in your routes. the resources :users appears first, and it has a match path that looks like users/:id and the users/search is incorrectly matching against that.
Just move the get 'users/search' to the top... or alternatively define it as a collection method under resources :users
resources :users do
collection do
get 'search'
end
Related
In view I have a link as follow in "teacher" controller. this link supposed to send an id to another controller "teacher_details" (1 to 1 relationship). This link open a webpage to add more details about teacher.
<%= link_to 'Add details', new_teacher_detail_path(#teacher), :id => "add_detail_link" %>
My controller code is
private
def set_teacher
#teachers = Teacher.find(params[:id])
end
When I run this code it shows me an error that "cannot find with out an ID". What am I doing wrong. The link does not passing the id parameter properly.
Route file is
root 'sessions#login'
get 'homes/home'
get '/login' => "sessions#login", :as => "login"
get '/logout' => "sessions#logout", :as => "logout"
get '/homes' => "homes#home"
resources :users
resources :sessions
resources :homes
resources :teachers
resources :teacher_details
resources :profiles
It seems like you are not nesting TeacherDetail with Teacher model in routing.
That's why it not getting any id parameter in new_teacher_detail_path. So you can't find params[:id] in below action.
private
def set_teacher
#teachers = Teacher.find(params[:id])
end)
Try change Your link_to as below :
<%= link_to 'Add details', new_teacher_detail_path(:id => #teacher.id), :id => "add_detail_link" %>
Now you will get ID parameter properly.
Routes file for teacher and details should be like this:
resources :teachers do
get 'details/new', on: :member
end
Then in view you can use link_to like this
<%= link_to 'Add details', details_new_teacher(#teacher) %>
No need to change in controller
In one-to-one relationship teacher_detail has foreign_key as teacher_id, So you can use like this
<%= link_to 'Add details', new_teacher_detail_path(id: #teacher.id), :id => "add_detail_link" %>
I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)
Inside app/views/participants/index.html.erb:
<%= form_tag bulk_add_participants_program_path do %>
<%= wrap_control_group do %>
<%= text_area_tag :bulk_add_participants, :size => "60x3" %>
<% end %>
<%= submit_tag "Import Participants and Users" %>
<% end %>
But notice that the controller and route pertain to the Program model (for good UI reasons). And I think that might be related to the problem. When I render that view I get this error message:
No route matches {:action=>"bulk_add_participants", :controller=>"programs"}
Which is weird because in app/controllers/programs_controller.rb:
def bulk_add_participants
puts "yay!" # because i am troubleshooting
end
And my config/Routes.rb is:
RepSurv::Application.routes.draw do
root to: 'programs#index'
devise_for :users, path_prefix: 'devise'
resources :users
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
get 'bulk_add_participants'
end
end
end
It's not finding the route because you have programs defined as a plural resource:
resources :programs do
When you do that and reference a member route like your bulk_add_participants, it expects a :program_id parameter in your case. (Try running rake routes, and you'll see a path like /programs/:program_id/bulk_add_participants.)
So your form_tag call should perhaps look like this:
<%= form_tag bulk_add_participants_program_path(#program) do %>
I'm using acts_as_taggable_on v.2.0.3 in Rails 3 to add tags to posts. I add a tag cloud as described here: https://github.com/mbleigh/acts-as-taggable-on, but I'm encountered an error:
ActionController::RoutingError in Posts#index: No route matches {:action=>"tag", :id=>"politics", :controller=>"posts"}. My code is below:
PostHelper:
module PostsHelper
include TagsHelper
end
Model Post:
class Post < ActiveRecord::Base
...
acts_as_taggable_on :tags
end
PostController
class PostController < ApplicationController
...
def tag_cloud
#tags = Post.tag_counts_on(:tags)
end
end
View:
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
Routes.rb:
Blog::Application.routes.draw do
root :to => "posts#index"
resources :posts do
member do
post :notify_friend
end
collection do
get :search
end
resources :comments
end
resources :users
resource :session
match '/login' => "sessions#new", :as => "login"
match '/logout' => "sessions#destroy", :as => "logout"
end
What am i doing wrong? Thanks for your answers.
Hmm, i think i understand.
First, i edited routes.rb in a such way:
resources :posts do
...
collection do
get :tag
end
end
Second, i added method "Tag" in the PostController:
def tag
#posts = Post.tagged_with(params[:id])
#tags = Post.tag_counts_on(:tags)
render :action => 'index'
end
It works!
For Rails4 #muki_rails approach did not work. This is what I did:
In routes.rb:
get 'tags/:tag' => 'articles#index', as: 'tag'
Now I can do this in the view (I am using slim):
- #article.tags.each do |tag|
= link_to tag.name, tag_path(tag.name)
Then in my ArticlesController if the params[:tag] variable is set, I search for all corresponding articles that match the given task.
def index
if params[:tag].present?
#articles = Article.published.tagged_with(params[:tag])
else
#articles = Article.published
end
end
I would like to create a mechanism for a User to keep track of other, favorite Users, similar to SO's favorite questions. I'm using the Rails 3.0 beta.
To do so, I have a User-Favorite HABTM relationship, which works as expected:
class User < ActiveRecord::Base
has_and_belongs_to_many :favorites, :class_name => "User", :join_table => "favorites", :association_foreign_key => "favorite_id", :foreign_key => "user_id"
end
The Favorites Controller only needs 3 of the 7 RESTful methods to manage a User's favorites:
class FavoritesController < ApplicationController
# GET /favorites
# GET /favorites.xml
def index
#user = User.find(params[:user_id])
#favorites = #user.favorites.joins(:profile).order("last_name,first_name")
...
end
def create
#favorite = User.find(params[:id])
current_user.favorites << #favorite
...
end
def destroy
#favorite = User.find(params[:id])
current_user.favorites.delete(#favorite)
...
end
end
The Routes.rb file contains the routing instruction:
resources :users, :except => :destroy do
resources :favorites, :only => [:index,:create,:destroy]
end
that generates these user-favorite routes:
GET /users/:user_id/favorites(.:format) {:controller=>"favorites", :action=>"index"}
user_favorites POST /users/:user_id/favorites(.:format) {:controller=>"favorites", :action=>"create"}
user_favorite DELETE /users/:user_id/favorites/:id(.:format) {:controller=>"favorites", :action=>"destroy"}
In the User's Show View, the User (#user) can be toggled as a favorite using image links, which works as expected:
<% if [test if user is a favorite] %>
# http://localhost:3000/favorites/destroy/:id?post=true
<%= link_to image_tag("favorite.png", :border => 0), :controller => :favorites, :action => :destroy, :post=>true, :id => #user %>
<% else %>
# http://localhost:3000/favorites/create/:id?post=true
<%= link_to image_tag("not-favorite.png", :border => 0), :controller => :favorites, :action => :create, :post=>true, :id => #user %>
<% end %>
However, in the current_user's favorite Index View, the link_to each favorite user:
# http://localhost:3010/users/4/favorites/3?post=true
<%= link_to image_tag("favorite.png", :border => 0), :controller => :favorites, :action => :destroy, :id => favorite, :post=>true %>
generates an error that reads:
No route matches "/users/4/favorites/3"
Questions:
Have I correctly specified my routing? Seem like the create and destroy routes would only need the id of the favorite, as the 'owner' of the favorite is always current_user.
If I'm simply referencing the Controller/Action in the Show view, do I even need the create/destroy routes?
Why doesn't the link_to in the Index View work correctly?
Are there any improvements that can be made to the over-all approach?
Your routing looks fine.
I think there is something wrong with your link_to, though. For one thing, the RESTful way is not to specify URLs with :controller and :action parameters. The correct way is using the generated URL methods, such as user_favorite_path. Also, you need to specify the :method parameter when targeting the destroy action. This is how I think the link_to should look like:
<%= link_to image_tag("favorite.png", :border => 0), user_favorite_path(#user, #favorite), :method => :delete %>
I believe the reason it says no route matches that URL is because you didn't specify the :method as :delete.
in your rake routes output, the paramater needed is :user_id not :id, so you need to send that in your link_to call.