2016-09-14 5 views
1

このエラーは、未定義メソッドの更新と言っています。表示と編集ボタンはうまく機能します。それは動作していない更新だけです。私はfriendly_idを使用しています。コントローラーはより良いルーティングで足場を取ります。ここで未定義のメソッドnilのための `update ':NilClass。 Friendly_idとScaffoldedコントローラ

は私のルートである:ここでは

Rails.application.routes.draw do 

    resources :static_pages, except: [:show, :edit] 

    devise_for :users 

    mount Bootsy::Engine => '/bootsy', as: 'bootsy' 

    get 'profile' => "users#profile" 

    root 'indexes#index' 

    get ':slug', to: 'static_pages#show' 

    get ':slug/edit', to: 'static_pages#edit' 

    # 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 'welcome#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 

はupdateメソッドの静的ページコントローラです:

:ここ

class StaticPagesController < ApplicationController 
    before_action :set_static_page, only: [:show, :edit, :update] 
    before_action :static_page_params, only: [:update, :create] 
    before_action :destroy_static_page, only: :destroy 
    before_filter :except => [:show] do 
    redirect_to :new_user_session unless current_user && current_user.role == 5 
    end 

    # GET /static_pages 
    # GET /static_pages.json 
    def index 
    @static_pages = StaticPage.all 
    end 

    # GET /static_pages/1 
    # GET /static_pages/1.json 
    def show 
    end 

    # GET /static_pages/new 
    def new 
    @static_page = StaticPage.new 
    end 

    # GET /static_pages/1/edit 
    def edit 
    end 

    # POST /static_pages 
    # POST /static_pages.json 
    def create 
    @static_page = StaticPage.new(static_page_params) 

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

    # PATCH/PUT /static_pages/1 
    # PATCH/PUT /static_pages/1.json 
    def update 
    respond_to do |format| 
     if @static_page.update(static_page_params) 
     format.html { redirect_to @static_page, notice: 'Static page was successfully updated.' } 
     format.json { render :show, status: :ok, location: @static_page } 
     else 
     format.html { render :edit } 
     format.json { render json: @static_page.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /static_pages/1 
    # DELETE /static_pages/1.json 
    def destroy 
    @static_page.destroy 
    respond_to do |format| 
     format.html { redirect_to static_pages_url, notice: 'Static page was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_static_page 
     @static_page = StaticPage.find_by_slug(params[:slug]) 
    end 

    def destroy_static_page 
     @static_page = StaticPage.find_by_slug(params[:slug]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def static_page_params 
     params.require(:static_page).permit(:title, :video_id, :content, :tags, :author, :date, :slug) 
    end 
end 

は、編集ページのためのスキャフォールドしてきた図であり、

%h1 Editing static_page 

= render 'form' 

= link_to 'Show', @static_page 
\| 
= link_to 'Back', static_pages_path 

編集:私のルートと関係があります。私はショーボタンを4つの鳴き声で動かすことができません。私は@ static_page.slugを試みますが、それは/ page_name/page_nameに連れて行きます。 @static_pageを実行すると、/ static_page/page_nameに移動します。私が必要とするのは、/ page_nameへ私を連れて行くことです。

編集2:ここでは、フォームが部分的である:

= simple_form_for(@static_page) do |f| 
    = f.error_notification 

    .form-inputs 
    = f.input :title 
    = f.input :video_id 
    .bootsy_text_area 
     = f.bootsy_area :content, :class => 'form-control', rows: 12 
    = f.input :tags 
    = f.input :author 
    = f.input :date 

    .form-actions 
    = f.button :submit 
+0

あなたの 'form.html.erb'を表示 –

+0

フォームの部分を追加しました – Jakxna360

答えて

1

あなたは@static_page.update(static_page_params)を呼んでいるが、この場合には、@static_pageはnilです。

@static_page@static_page = StaticPage.find_by_slug(params[:slug])と定義しましたが、そのスラッグを持つページが見つからない場合は、nilが返されます。

あなたがこれを扱うことができる2つの方法があります。

  • 使用find_by_slug!はレコードが見つからない場合に例外を上げる、またはなる、代わりに:
  • nilの存在を自分のためにチェックをし、どのように決めますあなたはそれを処理したい。
+0

私が抱えている問題は、更新をクリックすると/ static_pages/page_siteに持っていくことです。それはショーのように/ page_siteでそれを見つけるはずです。どのように私はこのサイトを指すのですか?同様に、@static_page = StaticPage.find_by_slug(params [:slug])がstaticpage/itemの代わりに/ itemにオブジェクトを見つける方法がありますか? – Jakxna360

+1

これは通常、あなたが従っているチュートリアルに戻って作業する部分です: – sevenseacat

+0

ありがとうございました。私はチュートリアルをredidし、find_by_slugを使っています! friendly_IDを使用しているときに働きました。 – Jakxna360

関連する問題