Rails - Couldn't find PackageRequest with 'id'=item_categories - ruby-on-rails

I have a package request model that handles all the tool loaning data. I am trying to have it so that when I go to the package request page in the application, that it shows only the form and not the index. So in the package request controller I tried to redirect the index path to the form path.
def index
#package_requests = PackageRequest.all
redirect_to new_package_request_path
end
But now when I try to go to a different page such as item categories from the package request form page I get this
error.
This kind of error shows up whenever I try to go to a different page from a form or show page.
log of server requests:
Started GET "/package_requests/new" for 127.0.0.1 at 2017-04-03 18:43:55 -0500
Processing by PackageRequestsController#new as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users"
ItemLocation Load (0.1ms) SELECT "item_locations".* FROM "item_locations"
Item Load (0.1ms) SELECT "items".* FROM "items"
Rendered package_requests/_form.html.erb (23.0ms)
Rendered package_requests/new.html.erb within layouts/application (24.1ms)
Rendered shared/_navbar.html.erb (0.1ms)
Completed 200 OK in 60ms (Views: 58.6ms | ActiveRecord: 0.4ms)
Started GET "/package_requests/items" for 127.0.0.1 at 2017-04-03 18:44:01 -0500
Processing by PackageRequestsController#show as HTML
Parameters: {"id"=>"items"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
PackageRequest Load (0.0ms) SELECT "package_requests".* FROM "package_requests" WHERE "package_requests"."id" = ? LIMIT 1 [["id", 0]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find PackageRequest with 'id'=items):
app/controllers/package_requests_controller.rb:68:in `set_package_request'
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (46.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (40.0ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /Users/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (89.4ms)
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
get "home", to: "static_pages#home"
root 'home#home'
resources :package_requests
resources :zips
resources :countries
resources :states
resources :cities
resources :item_statuses
resources :item_categories
resources :buildings
resources :tag_types
resources :vendors
resources :items
resources :item_locations
resources :loan_histories
resources :order_requests
resources :package_requests
resources :home
devise_for :users, controllers: {registrations: "registrations"}

Related

manage users by an admin in ruby on rails

how can I manage and edit other users profiles as an admin since I have one model and controller (users) ?
I tried to add a new action called updateusers
def updateusers
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
the problem here :it is updating my admin user with the other_user's data
stack trace
Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM "users" Rendered users/manage.html.erb within layouts/application (5.0ms) User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms)
'Started GET "/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms)
Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms)
Started PATCH "/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g", "email"=>"g#g.g", "role"=>"editor", "image"=>"pic.png", "admins"=>""}, "other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Unpermitted parameters: role, admins
(0.0ms) begin transaction SQL (1.0ms) UPDATE "users" SET "first_name" = ?, "last_name" = ?, "email" = ?, "updated_at" = ? WHERE "users"."id" = ? [["first_name", "g"], ["last_name", "g"], ["email", "g#g.g"], ["updated_at", "2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms)
If it's updating the wrong user, it means that params[:id] is the id of the user being updated. Are you passing the id of the user you want to update in the params? Try calling puts params.inspect at the top of the controller action to see what data is being passed. You need to look up #other_user with their id and you need to make sure that #other_user's id is being passed with the other form data.
after 10 days ,, Yes i did it - the solution is in the name of submit , I named the two submits with diffrent names <%= f.submit "update", name:"other" %>
then i used the update action like this
def update
if params[:current]
#user = current_user
if #user.update_attributes(user_params)
redirect_to '/profile'
else
redirect_to '/edit'
end
elsif params[:other]
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
end

how to manage users by an admin in ruby on rails

how can I manage and edit other users profiles as an admin since I have one model and controller (users) ?
I tried to add a new action called updateusers
def updateusers
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
the problem here :it is updating my admin user with the other_user's
data
stack trace
Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by
UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM
"users" Rendered users/manage.html.erb within layouts/application (5.0ms) User
Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1
[["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms)
'Started GET "/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by
UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered
users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200
OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms)
Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing
by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load
(1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms)
Started PATCH "/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by
UsersController#update as HTML Parameters: {"utf8"=>"✓",
"authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​
cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g",
"email"=>"g#g.g", "role"=>"editor", "image"=>"pic.png", "admins"=>""},
"other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users"
WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Unpermitted parameters: role, admins
(0.0ms) begin transaction SQL (1.0ms) UPDATE "users" SET "first_name" = ?,
"last_name" = ?, "email" = ?, "updated_at" = ? WHERE "users"."id" = ?
[["first_name", "g"], ["last_name", "g"], ["email", "g#g.g"], ["updated_at",
"2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected
to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms)
The user ID of the form in "editusers" is set to your admin (or logged in user). It's hard to say without seeing the code but I think you've set up the editusers form incorrectly. Perhaps using a hidden field to hold the ID of the user you want to update.
Try to avoid that and set up the #user object in the 'editusers' action #user = User.find(10)
Then in your view use a form_for #user do |f| without any hidden fields for the ID.
after 10 days ,, Yes i did it - the solution is in the name of submit , I named the two submits with diffrent names <%= f.submit "update", name:"other" %> then i used the update action like this
def update
if params[:current]
#user = current_user
if #user.update_attributes(user_params)
redirect_to '/profile'
else
redirect_to '/edit'
end
elsif params[:other]
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
end

Routes work in Development But not in Production

Weird Error. I have some routes that work perfectly during development but once i deploy and try to access them it comes up with page does not Exist error
I have the following routes.rb file:
TransportUnl::Application.routes.draw do
resources :trucks
resources :shipments do
collection do
get :autocomplete_location_cs
end
end
devise_for :users do
get '/users/sign_in' => 'devise/sessions#new'
get '/users/sign_out' => 'devise/sessions#destroy'
end
root :to => 'info#index'
resources :info do
collection do
get 'about'
get 'contact'
get 'you_dont_have_a_full_account'
get 'help'
end
member do
get 'index'
end
end
resources :companies
end
Not everything is setup yet. but i am getting a page not found error when i go to:
www.website.com/shipments
www.website.com/trucks
as well as others in production. The main index page works and you can login but these pages come up not found.
Production
Development
Production.log
Started GET "/shipments" for 108.235.52.160 at 2015-06-22 13:09:03 -0500
Processing by ShipmentsController#index as HTML
[1m[35m (0.6ms)[0m SELECT MAX("shipments"."price") AS max_id FROM "shipments"
[1m[36mShipment Load (0.3ms)[0m [1mSELECT "shipments".* FROM "shipments" [0m
Rendered shipments/_nav.html.erb (0.6ms)
Rendered shipments/_search_table.html.erb (0.1ms)
Rendered shipments/index.html.erb within layouts/application (1.2ms)
[1m[35mUser Load (0.5ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered shared/_header.html.erb (5.0ms)
Completed 500 Internal Server Error in 12ms
ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"companies"}):
app/views/shared/_header.html.erb:78:in `_app_views_shared__header_html_erb___2847381188393053217_232073740'
app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb__4421904906041360553_230384600'
app/controllers/shipments_controller.rb:7:in `index'
Started GET "/info/about" for 157.55.39.229 at 2015-06-22 13:10:29 -0500
Processing by InfoController#about as */*
Rendered info/about.html.erb within layouts/application (5.7ms)
Rendered shared/_header.html.erb (3.8ms)
[1m[36m (4.3ms)[0m [1mSELECT * FROM geometry_columns WHERE f_table_name='shipments'[0m
[1m[35mShipment Load (0.9ms)[0m SELECT "shipments".* FROM "shipments" ORDER BY id DESC LIMIT 3
Rendered shared/_footer.html.erb (31.6ms)
Completed 200 OK in 177ms (Views: 151.1ms | ActiveRecord: 25.6ms)
the Link is created like this:
<%= link_to "My Account", edit_company_path(current_user.company_id) %>
As you can see from logs, you got error page because of line 78 of app/views/shared/_header.html.erb file.
In this piece of code, where you creating link
<%= link_to "My Account", edit_company_path(current_user.company_id) %>
Check if company_id is not nil for that particular user. I'm pretty sure it's nil in your case.
As you can see from error logs, it tries to get action edit as collection action of companies controller - companies/edit. You don't have this route defined. But if current user will have company_id, the link will be built correctly and you wont receive an error.

redirect_to method propagates DELETE http action throughout nested routes

The above is the result of deleted comment. Notice that as you delete a comment, the comment's parent post is also deleted through redirect_to
Started DELETE "/posts/19/comments/30" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by CommentsController#destroy as JS
Parameters: {"post_id"=>"19", "id"=>"30"}
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1 [["id", "30"]]
(0.0ms) begin transaction
SQL (0.2ms) DELETE FROM "comments" WHERE "comments"."id" = ? [["id", 30]]
(7.7ms) commit transaction
Redirected to http://localhost:3000/posts/19
Completed 302 Found in 13ms (ActiveRecord: 8.4ms)
Started DELETE "/posts/19" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PostsController#destroy as JS
Parameters: {"id"=>"19"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
CACHE (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "19"]]
(0.0ms) begin transaction
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 19
SQL (0.2ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 19]]
(1.1ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 6ms (ActiveRecord: 1.7ms)
Started DELETE "/" for 127.0.0.1 at 2012-12-03 01:10:43 -0800
Processing by PagesController#home as JS
Rendered pages/home.html.haml within layouts/application (0.1ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 200 OK in 40ms (Views: 39.3ms | ActiveRecord: 0.2ms)
routes.rb
resources :posts do
member do
put "soft_destroy"
end
resources :comments do
member do
get "reply"
post "create_reply"
put "soft_destroy"
end
end
end
comments controller
def destroy
#post = Post.find(params[:post_id])
#comment = Comment.find(params[:id])
#comment.destroy
redirect_to #post
end
delete link on view file
= link_to "delete", [#post, comment], method: :DELETE, remote: true
Post model
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments
Comment model
belongs_to :post
Is there a reason why DELETE html verb propagates on posts controller as well? Rather than just calling show action?
The problem was caused by delete link on view file
= link_to "delete", [#post, comment], method: :DELETE, remote: true
For some reason, ajax request with method DELETE seems to propagate beyond the first DELETE request.
I removed remote: true and it now makes a GET request rather than DELETE request to the post.
= link_to "delete", [#post, comment], method: :DELETE
I still don't understand why this is happening though.

Github-like username url

I have this route that let me build custom url for users like
/thisismyname, and it works fine. But when I look at the log there is
something I don't like...
When I hit /gregory this is what is happenning:
1. Going to public_profile#public # Good
2. hitting /assets and trying to find a user with asset token # Not good
I thought my constraints would avoid this but it doesn't seem like it...
class PublicProfileConstraint
def self.matches?(request)
!['assets', 'admin'].include?(request.session[:token])
end
end
get "/:token" => "profiles#public", :as => :public_profile,
:constraints => PublicProfileConstraint
Here is the log:
Started GET "/gregory" for 127.0.0.1 at 2012-03-05 12:44:43 -0800
Processing by ProfilesController#public as HTML
Parameters: {"token"=>"gregory"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'gregorymarcilhacy' LIMIT 1
Rendered profiles/_modal.haml (0.1ms)
....
Rendered profiles/show.haml within layouts/application (154.7ms)
Completed 200 OK in 431ms (Views: 174.0ms | ActiveRecord: 5.5ms)
... Redering js files ...
# I DONT WANT THIS
Started GET "/assets/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Served asset - 404 Not Found (10ms)
Processing by ProfilesController#public as */*
Parameters: {"token"=>"assets"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'assets' LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 312ms
... Rendering images ...
# AND I DONT WANT THIS
Started GET "/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Processing by LandingController#landing as */*
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
Rendered landing/landing.haml within layouts/landing (0.8ms)
Completed 200 OK in 288ms (Views: 23.5ms | ActiveRecord: 2.2ms)
You are searching request.session for the token, but this will always fail as that's the session store rather than the request parameters. You probably want the equivalent of params[:token] in the constraint class. The request object documentation indicates that request.path_parameters[:token] might contain the value you are looking for.

Resources