我是铁杆新手,非常感谢你的帮助。
landingpg
,而不能查看homepg
& aboutpg
可以告诉我最好的方法吗?
class StaticPagesController < ApplicationController
respond_to :html, :xml, :json
def aboutpg
@timelines = Timeline.order("created_at DESC")
end
def homepg
@reviews = Review.limit(3).all
@search = Advert.search(params[:q])
@adverts = @search.result(distinct: true)
@companies = Company.all
end
def landingpg
@signup = Signup.new
end
end
route.rb
root 'static_pages#landingpg'
get 'about', to: 'static_pages#aboutpg'
get 'home', to: 'static_pages#homepg'
答案 0 :(得分:2)
您需要添加before_filter。
Class StaticPagesControlle < ApplicationController
before_filter :not_allowed ,:only => [:homepg,:aboutpg]
#rest is your code
end
#and in application controller or your StaticPageController do this
class ApplicationController < ActionController::Base
#define that method
def not_allowed
if current_user is unsigned (# here your logic for a user to be unsigend)
render json: {
status: 401,
message: 'You are not an authorised person to access this page'
}
end
end
答案 1 :(得分:1)
在调用您的操作之前,请包含将运行方法的before_filter
或before_action
。如果方法重定向到另一个路径,则会绕过对该操作的调用。
class StaticPagesController < ApplicationController
respond_to :html, :xml, :json
before_filter :user_allowed, only: [:aboutpg, :homepg]
def aboutpg
@timelines = Timeline.order("created_at DESC")
end
def homepg
@reviews = Review.limit(3).all
@search = Advert.search(params[:q])
@adverts = @search.result(distinct: true)
@companies = Company.all
end
def landingpg
@signup = Signup.new
end
def user_allowed
unless current_user
flash[:error] = "Please sign in first."
redirect_to root_path
end
end
end