NoMethod错误 - 为什么我收到此错误?

时间:2018-03-21 04:50:32

标签: ruby-on-rails ruby

Why am I getting this error message?

这是我的流派控制器代码:

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 %>

1 个答案:

答案 0 :(得分:4)

您正在使用form_for直接将html表单映射到对象@genre,因此rails要求满足以下条件

    流派表中的
  1. genres
  2. {li> genres Genre.rb模型中的attr_accessor。
  3. genres模型中名称Genre.rb的实例方法。
  4. 在你的情况下,所有三个都失踪了。

    导致问题的这一行

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

    我希望它能在某种程度上澄清你的问题。更好地提供有关您要实现的目标的更多详细信息,它可以帮助每个人以更好的方式回答。

    有一点可以肯定的是,在类型形式中输入genres字段是没有逻辑的。