Adding a new field to Rails model

前端 未结 4 1925
臣服心动
臣服心动 2021-01-30 11:03

I already created a scafold using

rails generate scaffold myOdb2 columna:integer, columnB:string

now I want to add columnc:string

4条回答
  •  一个人的身影
    2021-01-30 12:01

    no one mentioned updating strong parameters :

    So , let us say I have an existing scaffold called myapp and I want to add more fields to that scaffold . Three things to be done .

    The field to be added are :

    =>

    1) rails g migration add_term_count_and_current_record_count_and_previous_record_count_to_myapp term_count:integer , current_record_count:integer , previous_record_count:integer

    =>

    2) Update views, example updating _form.html.rb
    

    I needed to add :

    <%= f.label :current_record_count %>
    <%= f.number_field :current_record_count%>
    <%= f.label :current_record_count %>
    <%= f.number_field :previouse_record_count%>
    <%= f.label :term_count %>
    <%= f.number_field :terminations_count %>

    =>

    3) Update Controller : 
    

    New versions of rails has what is called strong parameter to prevent hackers from passing arbitrary column field values. Long story short , update the method with the new fields names , otherwise you will not see the new fields.. they wont get passed to anywhere...untrusted values ;o)

     # Never trust parameters from the scary internet, only allow the white list through.
    
    def vendor_file_params
        params.require(:vendor_file).permit(:name, :run_date,  :term_count ,
        :current_record_count , :previous_record_count ,:comments)   
    end
    end
    

提交回复
热议问题