Abhradip Abhradip - 2 years ago 85
Ruby Question

Issue regarding param is missing or the value is empty: information

I have an application of sign up and log in. After log in the users can see the contents of a database table. Now I want that after log in the users will also be able to insert data into another table via a form. Since I want to add this capacity after log in so I am writing the necessary code in the session part. But it is showing the following error:

ActionController::ParameterMissing in SessionsController#create

param is missing or the value is empty: information

My app/controllers/session_controller.rb file is:

class SessionsController < ApplicationController
def new
@info = Information.new

def index
user = User.authenticate(params[:email], params[:password])
@data = Information.all
@num = Information.count
@tym = Time.now.getutc

def create
user = User.authenticate(params[:email], params[:password])
@data = Information.all
@num = Information.count
@tym = Time.now.getutc
if user
session[:user_id] = user.id
# redirect_to root_url, :notice => "Logged in!"

# this part is for showing the content of the information table
respond_to do |format|
format.xml { render xml: @data }

@info = @num
@info = Information.new(information_params)

@currentUser = current_user.id
@usr = User.find_by(id: @currentUser)

@other_usr = User.where.not(id: @currentUser)


flash.now.alert = 'Invalid email or password'
render 'new'

def information_params
params.require(:information).permit(:age, :name)

def refresh_page
redirect_to :back

def destroy
@currentUser = current_user.id
@usr = User.find_by(id: @currentUser)

session[:user_id] = nil
redirect_to root_url, notice: 'Logged out!'

My app/views/sessions/create.html.erb file is:-

<h4>Database contents here:</h4>
<%= @currentUser %></br>
<% @tm=Time.new %>
<%= @tm.strftime("%H:%M:%S") %>
<% @data.each do |data| %>
<td><%= data.id %></td>
<td><%= data.age %></td>
<td><%= data.name %></td>
<% end %>


<%= form_tag 'refresh_page' do %>
<%= button_tag type: 'submit' do %>
<%= content_tag :div do%>
<h4>View Data </h4><h4><%= @num %></h4>
<% end %>
<% end %>
<% end %>

<!-- form for adding data into the information database -->
<%= form_for @info do |f| %>
<% if @info.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
<% end %>
<%= f.label :age %><br />
<%= f.text_field :age %>
<%= f.label :name %><br />
<%= f.text_field :name %>
<p class="button"><%= f.submit %></p>
<% end %>

I am using 2 tables here named User and Information. 'User' table holds all the information regarding the login credentials and I want to add records in the 'Information' table via the forms.

My routes.rb file is:

Rails.application.routes.draw do

get 'info_view/new'

get 'sessions/new'

#get 'users/new'

#get 'sign_up' => 'users#new' , :as => 'sign_up'

#root :to => 'users#new'
#resources :users

get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"

resources :users
resources :sessions

get 'refresh_page' => 'sessions#refresh_page'
post 'refresh_page' => 'sessions#refresh_page'

Answer Source

I think you're confusing create and new. You put your form in your create view, which is only rendered after processing the create endpoint in your controller. But since no params are passed to create (since you haven't even hit a form yet), it's generating an error.

Move your form to new.html.erb and access it by going to your /new route. After that your form should route the submission to your create endpoint. (At which point you can render a create view that just does something like confirm the object was created, or whatever you'd like.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download