2014-03-27 1 views
0

방금 ​​michael hartl 강의 9에서 railstutorial을 완료했습니다. localhost에서는 모든 것이 정상적으로 보이지만 heroku에서 응용 프로그램을 배포하면 일부 사용자가 사용자 목록의 잘못된 위치에 있습니다 (예 : User Example 첫 번째 페이지에 있어야하지만 두 번째 페이지에 있음). 왜 그렇게됩니까?localhost 및 heroku에 나열된 사용자의 차이점

내 위해 appliction : https://mordor-depth.herokuapp.com/users

당신은 노래-의 예를 사용하여 : [email protected] 는 foobar

바보 같은 질문 :에, 죄송합니다

users_controller.rb

class UsersController < ApplicationController 
    before_action :signed_in_user, only: [:index,:edit,:update,:destroy] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user, only: :destroy 
    def index 
    @users=User.paginate(page: params[:page]) 
    end 

    def show 
    @user=User.find(params[:id]) 
    end 

    def new 
    @user=User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     sign_in @user 
     flash[:success]="Welcome to the Sample App!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success]='User deleted!' 
    redirect_to users_url 
    end 

    def admin_user 
    redirect_to(root_url) unless current_user.admin? 
    end 

    def edit 

    end 

    def update 
    if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    private 
    def user_params 
     params.require(:user).permit(:name,:email,:password,:password_confirmation) 
    end 

    #Before filters 
    def signed_in_user 
     store_location 
     redirect_to signin_url, notice:'Please sign in.' unless signed_in? 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
    end 
end 

index.html.erb

<% provide(:title, 'All users') %> 
<h1>All users</h1> 
<%= will_paginate %> 

<ul class="users"> 
    <%= render @users %> 
</ul> 

<%= will_paginate %> 

_user.html.erb

<li> 
    <%= gravatar_for user, size:52 %> 
    <%=link_to user.name, user %> 
    <% if current_user.admin? && !current_user?(user)%> 
     | <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %> 
    <% end %> 
</li> 
+0

사용자 컨트롤러에있는 코드로 질문을 업데이트 할 수 있습니까? 실행중인 액션과 뷰에 대한 코드를 보면 흥미 롭습니다. app/controllers/users.rb 액션 인덱스 여야합니다. app/views/users/index.html.erb – rorra

+0

완료. 나는 코드를 레일 스터 리얼 (railstutorial)에서 볼 수 있다고 생각했었다 ... 나는 생각하기에, 코드 (로컬 애플 리케이션에서 잘 동작한다)는 다 괜찮아서 heroku에 관한 데이터베이스 문제 일 것임에 틀림 없다. – nuT707

+0

@ nuT707 컨트롤러의 색인 작업에서 레코드를 주문하지 않으므로 예제 사용자가 두 번째 페이지가 아닌 첫 페이지에 있어야한다고 생각하십니까? –

답변

2

당신은 당신이 어떤 주문 수행하지 않는 인덱스 액션을 볼 수 있습니다

def index 
    @users=User.paginate(page: params[:page]) 
end 

쉽게 이름으로 주문할 수 있습니다 일을

def index 
    @users = User.order(:name).paginate(page: params[:page]) 
end 
+0

감사합니다.하지만 로컬 목록에서 ID가 아닌 내 제품을 사용하는 이유는 무엇입니까? – nuT707

+1

+1 이것은 내가 말한 것입니다. 특정 순서로 레코드를 보려면 레코드를 정렬해야합니다. id 혼란으로 정렬에 관해서는이 질문을 볼 수 있습니다 http://stackoverflow.com/questions/824855/does-select-always-order-by-primary-key 귀하의 의심을 해결할 수 있습니다. –

+1

정렬을 지정하지 않으면 레코드 순서는 데이터베이스 엔진에 따라 다르며 데이터베이스 엔진이 heroku의 데이터베이스 엔진과 같지 않기 때문에 차이가 있습니다. ID로 주문하려면 **. order (: id) **를 사용하십시오. – rorra

관련 문제