关于更新记录的Ruby on Rails

时间:2016-04-29 19:23:07

标签: ruby-on-rails ruby

当我想更新学生记录时,我正在对更新操作执行补丁请求。但它正在检查student_id为NULL并且没有返回任何更新操作。任何帮助将不胜感激。提前致谢。 这是我的routes.rb,students_controller.rb和edit.html.erb

的routes.rb

.git

students_controller.rb

char object[sizeof(ClassName)];
new (&object) ClassName();

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

2 个答案:

答案 0 :(得分:1)

您的网址存在问题:

/student.5

应该是:

/student/5

用户Rails惯例,让您的生活更轻松。例如,在您的路线中使用resources :students或使用.find(:id)代替.find_by(id: X)等。

您可以通过自己指定表单操作网址来解决此问题:

<%= form_for(@student, :url => url_for(:controller => 'students', :action => 'update', params: {id: @student.id})) do |f| %>

但是,我建议通过正确按惯例设置路由来解决此问题。此问题将自动解决:

# routes.rb
resources :students

# edit.html.erb
<%= form_for @student do |f| %>

答案 1 :(得分:0)

根据您的日志,您的'id'似乎嵌套在'student'

"student"=> {"id"=>"5", "firstname" => "Raskesh", ....}

因此,在您的更新操作中,您应该尝试

@student = Student.find_by(id: params[:student][:id])

或更好......

@student = Student.find(params[:student][:id])