尝试添加自定义域时未定义的方法错误Find_by

时间:2016-03-15 14:47:44

标签: ruby-on-rails ruby

编辑似乎这个错误只会在以下情况下被抛出1.我添加了一个自定义域并尝试将其指向我的cname和2.当我在开发中和在根目录。

编辑2 根据建议,在类名中找到find_by我已将应用控制器更新为Page.find_by

现在我收到此错误

enter image description here

Schema.rb

ActiveRecord::Schema.define(version: 20160314190349) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "custom_domains", force: :cascade do |t|
    t.string   "value"
    t.integer  "page_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "custom_domains", ["page_id"], name: "index_custom_domains_on_page_id", using: :btree

  create_table "pages", force: :cascade do |t|
    t.string   "headline"
    t.string   "color"
    t.string   "buttoncolor"
    t.string   "buttontext"
    t.datetime "created_at",                            null: false
    t.datetime "updated_at",                            null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "user_id"
    t.integer  "main_referral_page_id"
    t.integer  "page_owner_referral_count", default: 0, null: false
    t.string   "subheadline"
    t.string   "subdomain"
    t.string   "ga_tracking_id"
    t.string   "mailchimp_list_name"
  end

  create_table "pages_subscribers", force: :cascade do |t|
    t.integer  "referral_count"
    t.integer  "referral_page_id"
    t.integer  "user_id"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "subscriber_id"
    t.integer  "page_id"
  end

  add_index "pages_subscribers", ["referral_page_id"], name: "index_pages_subscribers_on_referral_page_id", using: :btree
  add_index "pages_subscribers", ["user_id"], name: "index_pages_subscribers_on_user_id", using: :btree

  create_table "referral_pages", force: :cascade do |t|
    t.string   "navcolor"
    t.string   "rheadline"
    t.string   "rheadlinecolor"
    t.string   "rbackgroundcolor"
    t.string   "rboxcolor"
    t.string   "rcountcolor"
    t.string   "rhow"
    t.string   "rhowcolor"
    t.string   "rightheadline"
    t.string   "rightsub"
    t.string   "rightcolor"
    t.string   "bottomsub"
    t.string   "buynow"
    t.string   "buylink"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "page_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.string   "twitter"
    t.string   "facebook"
  end

  create_table "rewards", force: :cascade do |t|
    t.string   "name"
    t.integer  "level"
    t.float    "discount"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
    t.integer  "referral_page_id"
    t.integer  "referral_count_required"
    t.integer  "page_id"
  end

  add_index "rewards", ["page_id"], name: "index_rewards_on_page_id", using: :btree

  create_table "subscribers", force: :cascade do |t|
    t.string   "email"
    t.integer  "page_id"
    t.integer  "user_id"
    t.integer  "reference_count"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

  add_index "subscribers", ["page_id"], name: "index_subscribers_on_page_id", using: :btree
  add_index "subscribers", ["user_id"], name: "index_subscribers_on_user_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "reference_count",        default: 0,  null: false
    t.string   "role"
    t.string   "mailchimp_api_key"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

  add_foreign_key "custom_domains", "pages"
  add_foreign_key "rewards", "pages"
  add_foreign_key "subscribers", "pages"
  add_foreign_key "subscribers", "users"
end

例如,如果我导航到localhost300 / user / sign_in,我可以登录并使用该应用程序。

在下面添加我的路线文件。

我收到了一个奇怪的错误,似乎无法找到答案。

我已设置我的控制器以允许我的应用接受自定义域。我已经读到这可能是因为使用旧版本的导轨,但我相信我是最新的。

我正在跑步:

Rails 4.2.6

ruby​​ 2.2.2p95

错误发生在我的应用程序控制器的set_page方法中。

在我的pages_controller中调用此方法,因此错误消息显示它在那里发生,但该方法实际上存储在应用程序控制器中。

以下是错误消息

Error message

Application_Controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include Pundit
  before_filter :configure_permitted_parameters, if: :devise_controller?
  after_action :verify_authorized, unless: :devise_controller?  or :subscribers_controller?

  def home
  end

  def set_page
    # lookup domain and compage to actionpages.co env var
    if request.domain == ENV['APP_DOMAIN'] # actionpages.co
    #  look for a subdomain to exist
     if request.subdomain.present? && !request.subdomain.downcase.include?("www")
      @page = Page.find_by(subdomain: request.subdomain)
     end
    else
      # this is a custom domain look up the page for the custom domain
      @page = Page.find_by(custom_domain: request.domain)
      not_found unless @page.present?
    end
    authorize @page unless @page.nil?
  end

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :mailchimp_api_key, :password, :password_confirmation, :current_password) }
  end

  helper_method :subscriber_logged_in?
  def subscriber_logged_in?
    if cookies["subscriber_id"]
        return true
    else
        return false
    end
  end

  helper_method :share_id_for_subscriber_and_page
  def share_id_for_subscriber_and_page(subscriber, page)
    PagesSubscriber.share_id_for_subscriber_and_page(subscriber, page)
  end

end

Pages_controller.rb

# require File.expand_path('../boot', __FILE__)
require 'csv'
require 'rails/all'

class PagesController < ApplicationController
  before_action :set_page, only: [:show, :edit, :update, :destroy]

  layout "page", only: [:show]

  def index
    if current_user
      @pages = current_user.pages
      authorize @pages
    else
      set_page
      if @page.nil?
        # monster hack here, needs removed and better controller direction
        # for the root contoller. Passthrough options needed.
        authorize Page.new
        render 'static/home'
      else
        render 'pages/show.html.erb' unless current_user
      end
    end
  end

  def show
     @page ||= current_user.pages.find(params[:id])
     @referral_page = @page.referral_page
     @referrer_id = params[:referrer_id]
     @rewards = @page.rewards
     authorize @page
  end

  def new
    @page = current_user.pages.create(default)
    authorize @page

    respond_to do |format|
      if @page.save
        format.html { redirect_to @page, notice: 'Page was successfully created.' }
        format.json { render :show, status: :created, location: @page }
      else
        format.html { render :new }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end


  def create
    authorize @page
  end

  def edit
  end

  def update
    @page ||= current_user.pages.find(params[:id])
    authorize @page

    respond_to do |format|
      if @page.update(page_params)
        format.html { redirect_to @page, notice: 'Page was successfully updated.' }
        format.json { render :show, status: :ok, location: @page }
      else
        format.html { render :edit }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @page ||= current_user.pages.find(params[:id])
    authorize @page

    @page.destroy
    respond_to do |format|
      format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def downloadCSV
    @subscribers = Page.find(csv_params[:id]).subscribers
    respond_to do |format|
      format.csv { send_data @subscribers.to_csv }
    end
    authorize Page
  end


  private
    # Use callbacks to share common setup or constraints between actions.

    # def set_page
    #   @page = Page.find(params[:id])
    #   authorize @page
    # end
    #
    # def set_page(page = nil) # set page, optional page paramater for subdomains
    #   @page = page ? page : params[:id] ? Page.find(params[:id]) : Page.first
    #   @referral_page = @page.referral_page
    #   @referrer_id = params[:referrer_id]
    #   @rewards = @page.rewards
    #   authorize @page
    # end

    def default
      {
        :subdomain => Page.unique_subdomain,
        :headline => "Hover Over Any Element To See The Edit Button",
        :subheadline => "You can change the image, button color, button text, and the headline.",
        :color => "White",
        :buttoncolor => "warning",
        :buttontext => "Button Text",
        :image => nil,
        :user_id => current_user
      }
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def page_params
      params.require(:page).permit(:headline,
        :subheadline,
        :color,
        :buttoncolor,
        :buttontext,
        :image,
        :user_id,
        :subdomain,
        :ga_tracking_id,
        :mailchimp_list_name,
        custom_domains_attributes: [:id, :value, :_destroy])
    end

    def csv_params
      params.permit(:id)
    end

end


Routes.rb

Rails.application.routes.draw do

  get 'referrals/show'

  get 'test' => 'users#test_mailchimp'

  devise_for :users

  resources :rewards
  resources :subscribers
  resources :users
  get 'mailchimp' => 'users#mailchimp'
  post 'mailchimp' => 'users#update_mailchimp'

  resources :pages do
    resources :referral_pages do
    end
  end

  # Custom Routing
  get 'pages/:id/downloadCSV' => 'pages#downloadCSV', defaults: { format: 'csv' }
  get 'pages/:page_id/create_referral_page' => 'referral_pages#create_with_defaults'
  get 'pages/:id/:referrer_id' => 'pages#show'


  get "about" => "static#about"
  get "pricing" => "static#pricing"

  get 'refer/(:referrer_id)' => 'referrals#show', as: 'refer'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'pages#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

我整夜都被这个错误困住了!有什么想法吗?

1 个答案:

答案 0 :(得分:0)

缺少Page

def set_page
  # lookup domain and compage to actionpages.co env var
  if request.domain == ENV['APP_DOMAIN'] # actionpages.co
  #  look for a subdomain to exist
   if request.subdomain.present? && !request.subdomain.downcase.include?("www")
    @page = Page.find_by(subdomain: request.subdomain)
   end
  else
    # this is a custom domain look up the page for the custom domain
    #       vvvv MISSING THE Page HERE
    @page = Page.find_by(custom_domain: request.domain)
    not_found unless @page.present?
  end
  authorize @page unless @page.nil?
end

但是,根据您的新错误,您的custom_domain表格中没有pages字段。

检查您的schema.rb,如果您需要添加此字段,请为其创建迁移。

编辑:您有一个与custom_domains相关联的pages表,因此您需要以下内容:

def set_page
  # lookup domain and compage to actionpages.co env var
  if request.domain == ENV['APP_DOMAIN'] # actionpages.co
  #  look for a subdomain to exist
   if request.subdomain.present? && !request.subdomain.downcase.include?("www")
    @page = Page.find_by(subdomain: request.subdomain)
   end
  else
    # this is a custom domain look up the page for the custom domain
    custom_domain = CustomDomain.find_by(value: request.domain)
    @page = custom_domain.page if custom_domain
    not_found unless @page.present?
  end
  authorize @page unless @page.nil?
end