1
Sinatra 간단한 인증을 사용하여 사용자가 사이트에 액세스하기 전에 로그온하도록합니다. 그래서 홈 페이지는 로그온 할 것이고 그 후에 그것을 요구하지 않고 사이트를 사용할 수 있어야합니다. 지금은 로그온 후 홈 루트 페이지로 리디렉션 할 수있는 방법을 찾지 못했습니다. 세트 : 가정은해야대로 작동합니다. 싶은 것은 사용자를 제외한 모든 경로 이전에 로그인되어 있는지 확인하는 경우Sinatra 간단한 인증에서 직접 홈 경로 설정
# -*- coding: utf-8 -*-
# Required gems
require 'sinatra'
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'sinatra/simple-authentication'
require 'rack-flash'
# Required models and database
require './models/note'
class Index < Sinatra::Base
DataMapper::Logger.new($stdout, :debug)
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/recall.db")
use Rack::Flash, :sweep => true
register Sinatra::SimpleAuthentication
enable :sessions
set :home, '/'
# ** SHOW **
# Root to the index page
# Pull all notes from the DB into an instance varible to access from the index page in descends order.
get '/' do
login_required
@notes = Note.all :order => :id.desc
haml :index
end
# ** SAVE **
# Retrieves note contents from :contents on the index view and save them into the DB.
# Redirects back to the index page.
post '/' do
n = Note.new
n.content = params[:content]
n.created_at = Time.now
n.updated_at = Time.now
n.save
redirect '/'
end
# ** EDIT **
# Retrieves notes ID to edit the note
# Title varible is to display the note ID to the user to be able to edit/delete a specific note.
get '/:id' do
@note = Note.get params[:id]
@title = "Edit note ##{params[:id]}"
haml :edit
end
# Edit
# Retrieves the saved note for the user to edit then save it with the same ID and new timestamp
put '/:id' do
n = Note.get params[:id]
n.content = params[:content]
n.complete = params[:complete] ? 1 : 0
n.updated_at = Time.now
n.save
redirect '/'
end
# ** DESTROY **
# Delete note by the ID
# Retrun the note ID to the view page to confirm the deletion of the right note.
get '/:id/delete' do
@note = Note.get params[:id]
@title = "Confirm deletion of note ##{params[:id]}"
haml :delete
end
# Delte note by ID
delete '/:id' do
n = Note.get params[:id]
n.destroy
redirect '/'
end
# Check the completion of note (still not working)
get '/:id/complete' do
n = Note.get params[:id]
n.complete = n.complete ? 0 : 1 # flip it
n.updated_at = Time.now
n.save
redirect '/'
end
# To resturn the "Page not found" insted of the default Sinatra error page.
not_found do
halt 404, "Page not found 404"
end
end
**strong text**
게시물에서 추가 코드를 제거하십시오. 우리는 그 모든 것을 볼 필요가 없습니다. 또한 SO에 오신 것을 환영합니다! – thesecretmaster
도움을 주셔서 감사합니다, 불행히도 이것은 무한 루프를 만들었습니다. 명확하게하기 위해 사용자가 로그인없이 먼저 아무 것도 할 수 없도록하려는 경우 (색인 페이지를 보는 것조차도 안됨) – Faisal
하지만 사용자가 먼저 로그인해야합니까? 그들은 로그인 경로에 액세스해야합니다. – thesecretmaster