简体   繁体   English

NoMethod错误-为什么会出现此错误?

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

为什么会收到此错误消息?

This is my Genre controller code: 这是我的流派控制器代码:

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

And this is my form code : 这是我的表单代码:

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

You are using form_for which directly maps html form to object @genre , so rails expects either below one condition to be met 您正在使用form_for ,它将html表单直接映射到对象@genre ,因此rails希望满足以下一个条件

  1. genres column in genres table 类型表中的genres
  2. genres attr_accessor in Genre.rb model. Genre.rb模型中的genres attr_accessor。
  3. instance method by name genres in Genre.rb model. Genre.rb模型中按名称genres Genre.rb实例方法。

In your case all three missing. 在您的情况下,所有三个都不见了。

This line causing the issue 这行导致问题

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

I hope it clarifies your issue to some level. 我希望它可以在某种程度上澄清您的问题。 Better you provide more details on what you are trying to achieve, it helps everyone to answer in better way. 更好的是,您提供了有关要实现的目标的更多详细信息,它可以帮助每个人以更好的方式进行回答。

One thing for sure there is no logic in having genres input field in genre form. 可以肯定的是,以体裁形式输入genres输入字段没有逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM