lundi 11 avril 2016

undefined method `hirahana_value' for nil:NilClass

I try to understand what my buddy has coded to call in a show#view some instance variable. I have a problem to understand his code and call the method hirahana_value.

Here is my controller

class SymbolesController < ApplicationController
  before_action :set_symbole, only: [:show, :edit, :update, :destroy, :load_form, :load_attributes_form]


  def index
    @symboles = Symbole.all
  end

  def show
  end

  def new
    @symbole = Symbole.new
  end

  def edit
  end

  def create
    @symbole = Symbole.new()
    if symbole_params[:symbole_type].present?
      @symbole.build_kanji_attribute if symbole_params[:symbole_type] == "kanji"
      @symbole.build_hiragana_and_katagana_attribute if symbole_params[:symbole_type] == "hiragana_and_katagana"
    end
    @symbole.assign_attributes(symbole_params)

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

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

  def destroy
    @symbole.destroy
    respond_to do |format|
      format.html { redirect_to symboles_url, notice: 'Symbole was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def load_form
    @symbole = Symbole.new if @symbole.nil?
    if params[:form_name].present?
      respond_to do |format|
        format.js { render "ajax_form_"+params[:form_name] }
      end
    end
  end
  def load_attributes_form
    @symbole = Symbole.new if @symbole.nil?
    if params[:form_name].present?
      respond_to do |format|
        format.js { render "ajax_form_"+params[:form_name] }
      end
    end
  end


  private

    def set_symbole
      if params[:id].present?
        @symbole = Symbole.find(params[:id])
      else
        @symbole = Symbole.new
      end
    end

    def symbole_params
      params.require(:symbole).permit(:lang, :example_fr, :symbole_type, :css_class, kanji_attribute_attributes: [:value,:concept, :fr],
                                                                                     hiragana_and_katagana_attribute_attributes: [:hirahana_value,:katagana_value, :fr]
                                                                                     )
    end
end

And here is the new#view to create my object

<%= simple_form_for @symbole, :defaults => {:required => true} do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.hidden_field :lang, value: "Japonais"%>
    <%= f.select :symbole_type, options_for_select([["Kanji", "kanji"], ["Hiragana/Katagana", "hiragana_and_katagana"]]) , {:prompt => "Type de symbole", selected: @symbole.symbole_type}, {required: true} %>
    <div id="specific_attributes">
    </div>
    <%= f.input :example_fr, placeholder: "Exemple", label: "Exemple" %>
    <%= f.input :css_class, placeholder: "Nom de la classe css" , label: "Classe"%>

  </div>

  <div class="form-actions">
    <%= f.button :submit, "Valider" %>
  </div>
<% end %>

here is the show#view, I just want to call

<ul class="list-inline text-center card-frame">
          <li>
            <div class="card">
              <div class="front">
                <!-- PARAMETRER LE RESPONSIVE BOOTSTRAP -->
                  <!-- <div class="col-sm- col-xs-4 col-md-3"> -->


                <div class="card-hiragana hiragana-<%=@hiragana_and_katagana_attributes.hirahana_value.downcase.last%>">
                  <h1><b><%= @hiragana_and_katagana_attributes.hirahana_value %></b></h1>
                </div>
                <div class="card-katakana">
                  <p><%= @hiragana_and_katagana_attributes.hirahana_value %></p>
                </div>
                <!-- </div> -->
              </div>
              <div class="back">
                <div class="col-sm-3 col-xs-4 col-md-3 containerbackcards-<%=@hiragana_and_katagana_attributes.hirahana_value.downcase.last%>">
                    <div class="backcard-hiragana">
                      <h1><b><%= @hiragana_and_katagana_attributes.hirahana_value %></b></h1>
                    </div>
                    <div class="card-bigletter">
                      <p><%= @hiragana_and_katagana_attributes.hirahana_value %></p>
                    </div>
                </div>
              </div>
            </div>
          </li>
        </ul>

        <div class="text-center buttons-view-post">
          <% if current_user.try(:admin?) %>
            <%= link_to "Modifier l'hiragana", edit_hiragana_path(@hiragana), class: "btn btn-default btn-md" %>
            <% end %>
          <%= link_to "Voir tous les hiraganas", hiraganas_path, class: "btn btn-default btn-md" %>
        </div>
      </div>
    </div>
  </div>
</div>

Aucun commentaire:

Enregistrer un commentaire