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" %>
Related
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
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 have a button_to tag in my show.html.erb file.
<%= link_to 'Click HERE to open file', #user.image.url %><br/><br/><br/>
<%= label_tag(:q, "Parse CSV File:") %><br/>
<%= button_to 'Parse CSV', {:controller => "users_controller", :action => "process" } %>
<% end %>
Then I have this added to my users_controller.rb file
# GET /users/1/process
def process
puts 'To be Implemented'
end
Im getting an error in the routing file
No route matches [POST] "/assets"
This is how my routing file looks:
resources :users
resources :listings
What should I change. Im a bit confused, woould really appreciate some help.
Please correct your route and define like this
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
Then in route file
resources :users do
collection do
get: process
end
end
It will sure work
1) In view, use the controller name like 'users', not the 'users_controller' .
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
2) rails define few routes by default , but for other you need to define yourself .
Declare the routes like :
resources :users do
:member => {
:process => :get
}
end
Hope that help .
I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'
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.