lundi 23 mars 2015

How to save data in rails with no form?

I am new in rails and I am just learning the basics. This is my code on saving data


app/controllers/employee_controller.rb



class EmployeesController < ApplicationController

def index
render json: @employees = Employee.all
end

def show
render json: @employee = Employee.find(params[:id])
end

def new
@employee = Employee.new
end

def create
@employee = Employee.new(employee_params)

@employee.save
redirect_to @employee
end

private
def employee_params
params.require(:employee).permit(:fname, :mname, :lname, :contactno, :address, :username, :password)
end
end


app/views/employees/new.html.erb



<%= form_for @employee do |f| %>
<p>
<label>First Name</label><br>
<%= f.text_field :fname %>
</p>
<p>
<label>Middle Name</label><br>
<%= f.text_field :mname %>
</p>
<p>
<label>Last Name</label><br>
<%= f.text_field :lname %>
</p>
<p>
<label>Contact No.</label><br>
<%= f.text_field :contactno %>
</p>
<p>
<label>Address</label><br>
<%= f.text_area :address %>
</p>
<br>
<p>
<label>Username</label><br>
<%= f.text_field :username %>
</p>
<p>
<label>Password</label><br>
<%= f.text_field :password %>
</p>
<br>

<p>
<%= f.submit %>
</p>


But, my goal is to save right away without the html form. (NO INPUT) Like when I visit a certain URL and the values are automatically saved in the database. For a start, I would like to assign a constant value in every field just to see how it works.


Example,



  • fname='sample name'

  • mname='sampleMidName'

  • lname='sampleLastName' and etc...


How can I assign those values right away after a certain URL/site is visited. Can anyone help me? I'd so appreciate your help.


Aucun commentaire:

Enregistrer un commentaire