这是我的流派控制器代码:
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 %>
答案 0 :(得分:4)
您正在使用form_for
直接将html表单映射到对象@genre
,因此rails要求满足以下条件
genres
列genres
Genre.rb
模型中的attr_accessor。
genres
模型中名称Genre.rb
的实例方法。在你的情况下,所有三个都失踪了。
导致问题的这一行
<%= f.input :genres, required: true, input_html: { class: "input"}, wrapper: false, label_html: { class:"label" } %>
我希望它能在某种程度上澄清你的问题。更好地提供有关您要实现的目标的更多详细信息,它可以帮助每个人以更好的方式回答。
有一点可以肯定的是,在类型形式中输入genres
字段是没有逻辑的。