ajk4550
ajk4550

Reputation: 741

param is missing or the value is empty error

I am having trouble with the params.require().permit(). I have a User model from devise and a group model which represents a class (as in an academic class). When the user is on the groups index view I want them to be able to click join and become part of that group.

<%= form_for @student, :url => students_path(@student), method: :post do %>
    <%= hidden_field :student_id, :value => current_user.id %>
    <%= hidden_field :course_id, :value => group.id %>
    <%= submit_tag "+ Join", :class => "btn btn-primary pull-right join-button" %>
<% end %>

My thinking was I would pass the current users id as well as the group ID that they click join for as hidden values so there would just be a join button.

My group controllers index method looks like this

def index
    @groups = Group.all
    @student = Student.new
end

My Student Controller looks like this:

class StudentsController < ApplicationController
  before_action :set_student, only: [:show, :edit, :update, :destroy]

  def index
    #@students = Student.all
    @students = Student.where(:student_id => current_user.id)
    #respond_with(@students)
  end

  def show
    #respond_with(@student)
    @students = Student.find(params[:id])
  end

  def new
    @student = Student.new
    #respond_with(@student)
  end

  def edit
  end

  def create
    @student = Student.new(student_params)
    @student.save
    #respond_with(@student)
  end

  def update
    @student.update(student_params)
    #respond_with(@student)
  end

  def destroy
    @student.destroy
    #respond_with(@student)
  end

  private
    def set_student
      @student = Student.find(params[:id])
    end

    def student_params
      params.require(:student).permit(:course_id, :student_id)
    end
end

Whenever I try and submit the form (aka click the join button) I get an error saying:

param is missing or the value is empty: student

I also get this info which is correct as far as the student and group ID's

Request

Parameters:

{"authenticity_token"=>"/bJYZBGr6lfAzb9mYnvRfMZII+QS8iskd0MRuHh+RnE=",
 "course_id"=>"10",
 "student_id"=>"4"}

This also does insert into the database, but the student_id and course_id are nil. I am guessing this has something to do with strong params but I am not sure what I am doing wrong.

Upvotes: 2

Views: 1910

Answers (3)

hraynaud
hraynaud

Reputation: 736

It looks like your form_builder object is missing from the form_for block. Try this:

<%= form_for @student, :url => students_path(@student), method: :post do |f| %>
<%= f.hidden_field :student_id, :value => current_user.id %>
<%= f.hidden_field :course_id, :value => group.id %>
<%= submit_tag "+ Join", :class => "btn btn-primary pull-right join-button" %>
<% end %>

without the builder I don't believe rails will provide a student hash in the submitted params

Upvotes: 2

Art
Art

Reputation: 781

Since you're passing @student isn't the path just student_path(@student) rather than what you had, which is students_path(@student)?

Upvotes: 0

Kareem Hashem
Kareem Hashem

Reputation: 1077

in the before _action set_student
you are usin param[:id] , while your params don't have it .. I think you should use param[:student_id] instead

Upvotes: 1

Related Questions