簡體   English   中英

NoMethod錯誤-為什么會出現此錯誤?

[英]NoMethod Error - Why am I getting this error?

為什么會收到此錯誤消息?

這是我的流派控制器代碼:

class GenresController < ApplicationController
  before_action :set_genre, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]




    def index
        @genres = Genre.all.order("created_at desc")
      end


      def show
      end

      def new
        @genre = current_user.genres.build
      end

      def edit
      end


      def create
        @genre = current_user.genres.build(genre_params)

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

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


  def destroy
    @genre.destroy
    respond_to do |format|
      format.html { redirect_to genres_url, notice: 'Genre was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

      @genre = Genre.find(params[:id])
    end


    def genre_params
      params.require(:genre).permit(:artist, :album, :songs, :price)
    end
end

這是我的表單代碼:

<%= simple_form_for @genre, html: { multipart: true } do |f| %>
  <%= f.error_notification %>

    <div class="columns">

      <div class="field column is-9">
        <div class="control">
          <%= f.input :genres, required: true, input_html: { class: "input"}, wrapper: false, label_html: { class:"label" } %>
        </div>
      </div>

      <div class="field column">
        <div class="control">
          <%= f.input :artist, required: true, input_html: { class:"input", maxlength: 7  }, wrapper: false, label_html: { class:"label" } %>
        </div>
      </div>

    </div>

    <div class="field">
      <div class="control">
        <%= f.input :songs, required: true, input_html: { class:"input" }, wrapper: false, label_html: { class:"label" } %>
      </div>
    </div>

    <div class="field">
      <div class="control">
        <%= f.input :description, required: true, input_html: { class:"textarea" }, wrapper: false, label_html: { class:"label" } %>
      </div>
    </div>

    <div class="field">
      <div class="control">
        <label class="label">Add images</label>
          <div class="file">
          <label class="file-label">
            <%= f.input :image, as: :file, input_html: { class:"file-input instrument-image" }, label: false, wrapper: false %>
              <span class="file-cta">
                <span class="file-icon"><i class="fa fa-upload"></i></span>
                <span class="file-label">Choose a file…</span>
              </span>
          </label>
          </div>
        </div>
      </div>
      <output id="list"></output>
    <hr />

     <div class="field column">
        <div class="control">
          <%= f.input :price, required: true, input_html: { class:"input", maxlength: 7  }, wrapper: false, label_html: { class:"label" } %>
        </div>
      </div>

    <div class="field is-grouped">
      <div class="control">
        <%= f.button :submit, class: 'button is-warning' %>
        <%= link_to 'Cancel', genres_path, class:'button is-light' %>
      </div>
    </div>

  <% end %>

您正在使用form_for ,它將html表單直接映射到對象@genre ,因此rails希望滿足以下一個條件

  1. 類型表中的genres
  2. Genre.rb模型中的genres attr_accessor。
  3. Genre.rb模型中按名稱genres Genre.rb實例方法。

在您的情況下,所有三個都不見了。

這行導致問題

<%= f.input :genres, required: true, input_html: { class: "input"}, wrapper: false, label_html: { class:"label" } %>

我希望它可以在某種程度上澄清您的問題。 更好的是,您提供了有關要實現的目標的更多詳細信息,它可以幫助每個人以更好的方式進行回答。

可以肯定的是,以體裁形式輸入genres輸入字段沒有邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM