2012-11-28 2 views
-4

내가 만드는 새로운 user.My 컨트롤러 코드를 생성하기위한 PARAM 값에 액세스 할 수있는 작업ruby ​​on rails에서 컨트롤러의 param 값에 액세스하는 방법은 무엇입니까?

def newstudent 
    @student = Student.new(params) 
    puts "+++++++++++" 
    puts params.inspect 

    if @student.save 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render :json => @jtable } 
    end 
    end 
    end 

되어있어하지만이 작업을 수행하여 내가 terminal.It에서 일부 오류가이

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: action, controller): 
    app/controllers/students_controller.rb:42:in `new' 
    app/controllers/students_controller.rb:42:in `newstudent' 
처럼 보여줍니다 가지고 있었다

문제를 해결하는 데 도움을 주시겠습니까?

+0

폼 컨트롤러를 제출하면'id'.so 귀하의 경우는'@student = Student.find ([ID] PARAMS)처럼 할 수와 함께'create' 메소드를 호출 한 후 newstudent 메소드를 사용하여 새로운 레코드를 생성하면'@student = Student.new()'를 사용할 수 있습니다. –

+0

나는 그것을 위해 soluton을 가지고 있었다. – Niths

답변

5

이것은 신입생을위한 나의 컨트롤러입니다. 이 오류 메시지를 받으면 @student = Student.new(params.reject {|key,value| key =="controller" || key =="action"}) 코드를 사용하여 컨트롤러와 동작을 거부해야합니다. `당신이 .if :

def newstudent 
     @student = Student.new(params.reject {|key,value| key =="controller" || key =="action"}) 

     if @student.save 
      @jtable = {'Result' => 'OK','Record' => @student.attributes} 
     else 
     @jtable = {'Result' => 'ERROR','Message'=>'Empty'} 
     end 
     respond_to do |format| 
      format.html # new.html.erb 
      format.json { render :json => @jtable } 
     end 

     end 
0

학생 모델은 같이 attr_accessible를 사용하여 대량 할당 될 수있는 속성을 허용 해한다 : 기본적으로 더 대량 할당 레일 3.2.3에서

class Student < ActiveRecord::Base 
    attr_accessible :name 
end 
+0

감사합니다. 나는 해결책을 가졌다. 당신은 그것을 확인할 수 있습니다. – Niths

+0

오케이. 고마워요. – Niths

1

없다. 당신은 모델로 가서 attr_accessible : name, : position, : visible을 추가해야합니다. 기본적으로 대량 할당하려는 모든 속성을 추가해야합니다.

class Student < ActiveRecord::Base 
    attr_accessible :name, :position, :visible 
end 
+0

감사합니다. 나는 해결책을 가졌다. 당신은 그것을 확인할 수 있습니다. – Niths

관련 문제