vendredi 31 juillet 2015

ActionController::ParameterMissing in SubjectsController#update

I'm a beginner in RoR. I'm taking the course in lynda.com by Kevin. I'm doing the CRUD in RoR. When I do delete operation it is showing the error

ActionController::ParameterMissing in SubjectsController#update

param is missing or the value is empty: subject

subjects_controller.rb

class SubjectsController < ApplicationController

def index
    render('index')
end

def list
    @subjects = Subject.order("subjects.position ASC")
end

def show
    @subject = Subject.find(params[:id])
end

def new
    @subject = Subject.new(:name => 'username')
end

def create
    @subject = Subject.new(params.require(:subject).permit(:name, :position, :visible))
    if @subject.save
        redirect_to(:action => 'list')
    else
        render('new')
    end
end

def edit
    @subject = Subject.find(params[:id])
end

def update
    @subject = Subject.find(params[:id])
    if @subject.update(subject_params)
        redirect_to(:action => 'show', :id => @subject.id)
    else
        render('edit')
    end
end

def delete
    @subject = Subject.find(params[:id])
end

def destroy
    @subject = Subject.find(params[:id])
    @subject.destroy
    redirect_to(:action => 'list')
end

private
def subject_params
  params.require(:subject).permit(:id, :name, :position, :visible)
end

The highlighted lines of error are

if @subject.update(subject_params)

and

 params.require(:subject).permit(:id, :name, :position, :visible)

delere.html.erb

<%= link_to("<<Back to List", {:action => 'list'}, :class => 'back-link') %>
<%= form_for(:subject, :url => {:action => 'destroy', :id => @subject.id}) do |f| -%>
    <p>Are you sure that you want to delete this subject permanently?</p>
    <p class="reference-name"><%= @subject.name %></p>

    <div class="form-buttons">
        <%= submit_tag("Delete Subject") %>
    </div>
<% end %>

edit.html.erb

<%= link_to("<<Back to List", {:action => 'list'}, :class => 'back-link') %>

<%= form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>

    <table summary="Subject from fields">
        <tr>
            <th>Name</th>
            <td><%= f.text_field(:name) %></td>
        </tr>
        <tr>
            <th>Position</th>
            <td><%= f.text_field(:position) %></td>
        </tr>
        <tr>
            <th>Visible</th>
            <td><%= f.text_field(:visible) %></td>
        </tr>
    </table>

    <div class="form-buttons">
        <%= submit_tag("Update Subject") %>
    </div>
<% end %>

Any ideas? Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire