vendredi 17 juin 2016

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.

Aucun commentaire:

Enregistrer un commentaire