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
genres
column in genres table genres
attr_accessor in Genre.rb
model. genres
in Genre.rb
model. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.