will_paginate confusing urls with devise authenticated root routes - ruby-on-rails

I have the following in my routes.rb:
...
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
end
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
...
will_paginate is like this:
<%= will_paginate #collection, renderer: BootstrapPagination::Rails %>
And the result links are like this:
http://localhost:3000/?page=2
If I make unauthenticated root be other thing than editions#index, the links are correct, like this:
http://localhost:3000/games?page=2
I've tried using this:
<%= will_paginate #collection, :params => {:controller => '/editions', :action => 'index'}, renderer: BootstrapPagination::Rails %>
But it does not force will_paginate to use the right route. And if I try this:
<%= will_paginate #collection, :params => {:controller => editions_path }, renderer: BootstrapPagination::Rails %>
Rails give me route errors:
No route matches {:action=>"index", :controller=>"games", :page=>2}

I've inverted my routes.rb and it works now. It seems order is important and I was not aware.
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
end

Related

RoR link to specific controller in routes.rb

I use Ruby 2.0 and Rails 4.2.0. I can't create link for simple_form_for in _form.html.erb. My code:
routes.eb
Rails.application.routes.draw do
namespace :admin, path: 'uceadmin' do
root :to => 'dashboard#index'
namespace :newsletter, path: 'newsletter' do
resources :lists, controller: 'newsletter_lists' do
resources :subscribers, controller: 'newsletter_subscribers'
end
end
end
end
app\views\admin\newsletter_subscribers_form.html.erb
<div class="row">
<%= simple_form_for([:admin, :newsletter, #list, #newsletter_subscriber]) do |f| %>
rake routes:
And what I have in result:
undefined method `admin_newsletter_newsletter_list_newsletter_subscribers_path' for #<#<Class:0xd1d5af0>:0xdc2a7b8>
I guess you should use the :url option to pass URL for submitting. In your case it is admin_newsletter_list_subscribers_path, as I can see.
<%= simple_form_for #newsletter_subscriber, url: admin_newsletter_list_subscribers_path(#list) do |f| %>

Couldn't find User with id=search

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

Edit User Info Using Devise and Devise_Invitable

I'm struggling with this issue for the past week or so, and I've tried everything I could think of so I need your help..! I'm using devise and devise invitable
I've created a page to edit user info like username, firstname, lastname...
# /controllers/settings_controllers.rb
class SettingsController < ApplicationController
def info
#user = current_user
end
end
# /controllers/users_controllers.rb
class UsersController < Devise::SessionsController
def update
#user = User.find(current_user.id)
if #user.update_attributes(user_params)
redirect_to :back
end
end
end
# /views/settings/info.html.erb
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :firstname %>
<%= f.text_field :firstname %>
....
<% button_value = "Update" %>
<% end %>
My routes :
devise_for :users ,:controllers => { :invitations => 'users/invitations' }
resources :users, only: [:edit, :update]
devise_for :users, :skip => [:registrations]
as :user do
get 'user/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'user' => 'devise/registrations#update', :as => 'user_registration'
end
devise_scope :user do
authenticated :user do
root :to => 'aggregator#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
get "users/new" => "users#new"
get "users/:id" => "users#show"
end
match 'settings/info' => 'settings#info', :as => 'info'
When I try to update the form, I have the following error (my user id is 1) :
Could not find devise mapping for path "/users/1"
Edit
So I've put resources :users, only: [:edit, :update] after devise_for :users, like suggested by #coletrain and error is gone. But it redirects to my profile /users/1 when I want to be redirected to /settings/info and more importantly, it does not update my info...
My guess is that update method in users_controller is not reached.
In the routes.rb:
add put "users/:id" => "users#update" inside devise_scope :user do ... end block.
Also:
In user_controller update method, change #user.update_attributes(user_params) to #user.update_attributes(params["user"])
I had the same problem. I think the simplest solution is this: Just use what devise gives you by default.
Routes:
devise_scope :user do
get "account", to: "devise/registrations#edit"
patch "account", to: "devise/registrations#update"
put "account", to: "devise/registrations#update"
delete "account", to: "devise/registrations#destroy"
end
/views/devise/registrations/edit.html.erb #generated by devise
replace the path like this:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
to this (because I named the routes "account" in this example)
<%= form_for(resource, as: resource_name, url: account_path, html: { method: :put }) do |f| %>
Note that you have to delete resource_name too. Otherwise there will be routing problems after you committed changes

Rails route not finding corresponding action

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 %>

Devise logged in root route rails 3

Heyya guys.
So i thought about this coolio idea, if you are logged in then you get some sort of dashboard, else you get an information/login/sign up page.. So how do i do that..
I mostly wants to do this in Routes = not something like
def index
if current_user.present?
render :action => 'logged_in'
else
render :action => 'logged_out'
end
end
thanks in advance!
/ Oluf Nielsen
Think you may have been looking for this:
authenticated :user do
root :to => "dashboard#show"
end
root :to => "devise/sessions#new"
Note: it's authenticate*d*
I too wanted this in my app, here's what I came up with.
MyCoolioApp::Application.routes.draw do
root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
root :to => 'welcome#index'
get "/" => 'users#dashboard', :as => "user_root"
# ..
end
In Rails 3 you can use Request Based Contraints to dynamically map your root route. The solution above works for the Devise authentication gem but can be modified to support your own implementation.
With the above root_path or / will route to a WelcomeController#index action for un-authenticated requests. When a user is logged in the same root_path will route to UsersController#dashboard.
Hope this helps.
I have the same problem and I solved it with this:
authenticated :user do
root :to => "wathever#index"
end
unauthenticated :user do
devise_scope :user do
get "/" => "devise/sessions#new"
end
end
Hope it helps.
are you using devise's before filters?
class FooController < ActionController::Base
before_filter :authenticate_user!
...
Why don't you try altering the default login views so they have the info/login/signup infos you want.
Here's what I'm using in my application layout file right now. Haven't broken it out into partials yet:
<% if user_signed_in? %>
<%= current_user.email %> |
<%= link_to "Logout", destroy_user_session_path %>
<% else %>
<%= link_to "Login", new_user_session_path %> |
<%= link_to "Register", new_user_registration_path %>
<% end %>

Resources