2016-10-27 4 views
0

私はRails 5アプリケーションで名前空間を使用する方法を学びたいと思っています。Rails - ビュー内の名前空間付きテーブル属性を参照する方法

私は組織モデルを持っており、フォルダ名「スタンス」の下に一連のネストされたモデルを作成しました。これらのモデルの1つを概要と呼びます。

団体は以下のとおりです。

Organisation.rb

has_one :overview, class_name: Stance::Overview 
    accepts_nested_attributes_for :overview, reject_if: :all_blank, allow_destroy: true 

スタンス::スタンスというフォルダの下にネストされているスタンスリソースの

class Stance::Overview < ApplicationRecord 
    belongs_to :organisation, inverse_of: :overview 

私のコントローラの概要。

私のルートは以下のとおりです。私の立場で

namespace :stance do 
    resources :overviews 
    end 

が部分図、私は、概要テーブルから属性をレンダリングしようとしています。

私が試してみました:

<p><%= @overview.internal_explanation %></p> 
<p><%= @stance_overview.internal_explanation %></p> 
<p><%= @stance.overview.internal_explanation %></p> 
<p><%= @stance::overview.internal_explanation %></p> 

を私は自分の組織のショーでは、この部分を表示したいです。私はそれをしようとしています:

<%= render 'stance/overviews/internal', overview: @overview %> 

しかし、私は概要テーブルにアクセスする方法を把握することはできません。私は団体に「スタンス」への参照を追加する必要がありますか?

私はコンソールに私が書く必要があることがわかります。

o = Stance::Overview.create(internal_explanation: "test") 
o = Stance::Overview.first 

が、私はコード自体にそれを使用する方法を見ることができません。

コンソールにこの属性のレコードがあることがわかります。

スキーマ内のテーブルの名前は「stance_overview」です。

私の組織・コントローラがあります。

class OrganisationsController < ApplicationController 
    before_action :set_organisation, only: [:show, :edit, :update, :destroy] 

    def index 
    @organisations = Organisation.all 

    end 

    def show 

    end 

    def new 
    @organisation = Organisation.new 
    @organisation.build_overview 

    end 

    def edit 
    @organisation.build_overview unless @organisation.overview 
    end 

    def create 
    @organisation = Organisation.new(organisation_params) 

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

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

    def destroy 
    @organisation.destroy 
    respond_to do |format| 
     format.html { redirect_to organisations_url, notice: 'Organisation was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 




private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_organisation 
     @organisation = Organisation.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def organisation_params 
     params.fetch(:organisation, {}).permit(:title, :comment, 
     overview_attributes: [:internal_explanation, :external_explanation ] 

     ) 
    end 


end 

として、私はまた、組織のための強力なのparamsを定義しようとしている:

undefined method `internal_explanation' for nil:NilClass 

を:私が言うエラーが出続ける

stance_overview_attributes: [:internal_explanation, :external_explanation ] 

誰でも私のアプリケーションに名前空間を使用する方法を学ぶのを助けるために教材を紹介してくれますか?私はこの知識の基礎を理解しようとしているので、知識を詰めることができます。私は試行錯誤で物事を見つけていますが、実際に何が必要なのか理解していません(この場合、私の試行はありません)。

+0

あなたは '@ overview'をどこで設定していますか?実行時にnilと表示されます。 –

+0

'@stance.overview.internal_explanation'これは、関連する関連する概要(存在する場合)から値を表示するための正しいものでなければなりません。しかし、あなたがこれを使うなら、 '<%= render 'stance/overviews/internal'、overview:@overview%>'そのローカル変数は 'overview'変数を渡したことを意味します。 '@ overview'変数(もしあれば)の前に' 'overview.internal_explanation'のように直接使用することができますが、' @ overview'に何もなかったらそれはうまく動作しません。パス: 'overview:@stance.overview' –

+0

ありがとう@TarynEast - ある日私はこれをどのように考えるかを考え出す – Mel

答えて

1

Overviewモデル(表)にアクセスする場合は、Stance名前空間にいない場合は、Stance::Overviewを使用する必要があります。たとえば、Stance名前空間にあるコントローラで作業している場合は、アクセスにはちょうどOverviewを使用できます。

リレーションからアクセスするには、追加の表記は不要です(@organisation.overview)。

私はあなたが

<%= render 'stance/overviews/internal', overview: @organisation.overview %>

、あなたが@なしoverviewを使用する必要があり、部分的のようにあなたの部分を宣言する必要が小文字あなたに正しく理解していれば。

関連する問題