Abhradip Abhradip - 5 months ago 12
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
super
@info = Information.new
end

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

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.html
format.xml { render xml: @data }

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

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

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

@usr.send_password_reset_1

@other_usr.each(&:notloggedin)
end
else
flash.now.alert = 'Invalid email or password'
render 'new'
end
end

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

def refresh_page
redirect_to :back
end

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

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


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

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

<div>

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

<!-- 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>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :age %><br />
<%= f.text_field :age %>
</p>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<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'
end

Answer

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.)

Comments