Reputation: 31
When i want to update a student record, i am doing the patch request to the update action.But it is checking the student_id with NULL and not returning anything to update action.Any help would be greatly appreciated. Thanks in advance. Here are my routes.rb, students_controller.rb and edit.html.erb
root 'static_pages#home'
get 'student' => 'students#home'
get 'addstudent'=> 'students#new'
get 'liststudent'=> 'students#index'
get 'updatestudent' => 'students#input'
get 'deletestudent' => 'students#inputDelete'
patch 'student' => 'students#update'
post '/search', to: 'students#edit', as: :search
post '/searchDelete', to: 'students#delete', as: :searchDelete
class StudentsController < ApplicationController
def home
end
def index
@students = Student.all
end
def new
@student = Student.new
end
def create
@student = Student.new(student_params)
#render json: @student
if @student.save
flash[:success]="student added successfully"
redirect_to new_student_path
else
flash[:error]="Error while adding student"
render "new"
end
end
def input
end
def updateHome
@student = Student.find_by(student_id: params[:q])
render 'edit'
end
def edit
@student = Student.find_by(student_id: params[:q])
#render json: @student
render 'edit'
#if @student.update_attributes(student_params)
# render json: @student
#flash[:notice]="upate successful"
#redirect_to "student_path"
#else
# flash[:notice]="update not successful"
# redirect_to "student_path"
#end
end
def update
# render text: "this is update action"
@student = Student.find_by(id: params[:id])
#@student.category = params[:category]
#if @student
render json: @student
#end
# if @student.update_attributes(student_params)
# render json: @student
#flash[:notice]="upate successful"
#redirect_to "student_path"
# else
# render text: "went to else"
# flash[:notice]="update not successful"
# redirect_to "student_path"
# end
end
def inputDelete
end
def delete
@student = Student.find_by(student_id: params[:q])
#render json: @student
#render text: "This is delete action"
@student.destroy
#Student.find_by(student_id: params[:q]).destroy
flash[:success]="student delete successfully"
redirect_to "student_path"
end
def student_params
params.require(:student).permit(:firstname, :lastname, :student_id, :category, :gender, :phone, :dob, :program_id)
end
end
<h1>Update student profile</h1>
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for @student do |f| %>
<%= f.label :RecordId %>
<%= f.text_field :id, class: 'form-control' %>
<%= f.label :FirstName %>
<%= f.text_field :firstname, class: 'form-control' %>
<%= f.label :LastName %>
<%= f.text_field :lastname, class: 'form-control' %>
<%= f.label :Student_Id %>
<%= f.text_field :student_id, class: 'form-control' %>
<%= f.label :Category %>
<%= f.text_field :category, class: 'form-control' %>
<%= f.label :Gender %>
<%= f.text_field :gender, class: 'form-control' %>
<%= f.label :PhoneNumber %>
<%= f.text_field :phone, class: 'form-control' %>
<%= f.label :DateOfBirth %>
<%= f.text_field :dob, class: 'form-control' %>
<%= f.label :Program_Id %>
<%= f.text_field :program_id, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Started PATCH "/student.5" for 127.0.0.1 at 2016-04-30 00:27:35 +0530
Processing by StudentsController#update as
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GlI8TPiB5Ua5XWH3Hwu3ycuhdaCFxST8rLQIuyJ
7AlXGXeAQWN0IcD/iS04lXBhliMCSWvyYif4X5TMHupwDrg==", "student"=> {"id"=>"5", "firstname"=>"Rakesh", "lastname"=>"ALLAM", "student_id"=>"cs13b1002", "category"=>"obc", "gender"=>"Male", "phone"=>"8790352136", "dob"=>"1995-09-26", "program_id"=>"btech"}, "commit"=>"Save changes"}
Student Load (0.6ms) SELECT "students".* FROM "students" WHERE "students"."id" IS NULL LIMIT 1
Completed 200 OK in 3ms (Views: 0.3ms | ActiveRecord: 0.6ms)
Upvotes: 1
Views: 59
Reputation: 51
Based on your logs, it seems your 'id' is nested within 'student'
"student"=> {"id"=>"5", "firstname" => "Raskesh", ....}
Therefore, in your update action you should try
@student = Student.find_by(id: params[:student][:id])
or better yet...
@student = Student.find(params[:student][:id])
Upvotes: 0
Reputation: 11813
You have a problem with your URL:
/student.5
It should be:
/student/5
User Rails conventions and make your life easier. For example, use resources :students
in your routes or use .find(:id)
instead of .find_by(id: X)
, etc.
You can resolve this problem by specifying form action url yourself:
<%= form_for(@student, :url => url_for(:controller => 'students', :action => 'update', params: {id: @student.id})) do |f| %>
But, I would suggest resolving this problem by correctly setting up your routes by convention. This problem will be resolved automatically:
# routes.rb
resources :students
# edit.html.erb
<%= form_for @student do |f| %>
Upvotes: 1