I'm using devise/Ruby on Rails and when I try to open my localhost I get the following infinite loop, and have no idea why since the only change I've made was creating seeds using the Faker gem, and creating a show view for when the user logs in.
At first I thought it was something wrong with my if/else statement in my controller, since when I reseed the data, there's no current_user signed in and it should take me to the new user registration path created through devise, but was lost as to why it repeats itself..
Any and all help would be greatly appreciated!
Below is terminal:
Started GET "/users/sign_in" for 127.0.0.1 at 2017-12-30 13:53:58 -0800
Processing by UsersController#show as HTML
Parameters: {"id"=>"sign_in"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
UsersController
class UsersController < ApplicationController
before_action :authenticate_user!
def show
if current_user.present?
@user = current_user
@item = Item.new
@items = @user.items
else
redirect_to new_user_registration_path
end
end
end
Users#show
<h2> Your to-do list</h2>
<div class="col-md-8">
<div class='items'>
<%= render partial: "items/item", local: { item: @item} %>
</div>
</div>
<div class="col-md-8">
<div class='new-item'>
<%= render partial: "items/form", local: { item: @item } %>
</div>
</div>
Routes:
Rails.application.routes.draw do
get 'welcome/index'
get 'welcome/about'
root 'users#show'
resources :users do
resources :items, only: [:new, :create, :index]
end
devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
能够找出根本问题,即 routes.rb 中代码的放置顺序。
你应该在你的路线中有devise_for :users
。
You have already permission denied for guest user I mean which user not signed in using devise this before_action :authenticate_user!
this means user not accesing any action from UsersController
which not signed in after that you have redeclared permission on show
action if current_user.present?
which is not actually needed then show
action should something like this
@user = current_user
@item = Item.new
@items = @user.items
Remove the if
else
condition.
Hope to help
In my case:
users_controller.rb
before_action :logged_in?, only: :new
def new
@user = User.new
render layout: "session"
end
and
application_controller.rb
def logged_in?
redirect_to users_new_url unless current_user.present?
end
When I was trying to redirect to the 'users/new' page, the same error occurred.
This is just because I'm trying to redirect to the 'users/new' page and def logged_in? is also redirecting to the same page.
Then I changed the application_controller.rb code like this:
def logged_in?
redirect_to root_url unless current_user.blank?
end
Error_Resolved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.