Default Values ​​for Model Fields on a Ruby on Rails Form

I have a Model that has username, data, tags, date, votes fields. I have a form using form_for that creates a new element and puts it in the database. However, as you can guess, I want the vote field to be 0 and the date field to be the current date when it is placed in the database. How and where can I set / apply these values ​​to an element?

I can get it to work with hidden fields in the form, but this is due to obvious problems (someone can set the votes field to a massive number).

+4
source share
5 answers

Just use the default value; zero, for votes in db, use automatic timestamps ( created_at ) instead of date and fields in the form only for the parameters you set. Remember to protect sensitive attributes.

 class CreateModels < ActiveRecord::Migration def up create_table :models do |t| t.string :username t.text :data t.string :tags t.integer :votes, :default => 0 t.timestamps # this will give you two automatic fields: created_at and updated_at end end … end class Model < ActiveRecord::Base attr_protected :votes #so that it cannot be set by mass assignment … end 
+10
source

I can confirm that the DB constraint approach is one of the best. But it is not always possible to use. Assume that there is inheritance from one table and a different default value is required for each child model. Than I recommend putting this in the model. Let me give an example:

 class ChildModel < Model after_initialize :set_defaults private def set_defaults self.allowed_votes_per_person = 10 if self.new_record? end end 

Using the: after_initialize callback, there is no need to create and remember the call: new_default like methods, etc. It sets the required defaults, but remains unobtrusive in terms of interface when you call ChildModel.new

+4
source

One easy way is to set the default values ​​for the create action on the controller.

 @model = Model.new(params[:model]) @model.votes = 1 @model.date = Time.now @model.save 

Another way and cleaner is also to create a method in the Model.

 class Model def new_default(model) model = Model.new(model) model.votes = 1 model.date = Time.now end 

So, you have a controller:

 @model = Model.new_default(params[:model]) if @model.save render something else render something_else end 
+3
source

Here is the attribute protection screencast: http://railscasts.com/episodes/26-hackers-love-mass-assignment

Rails models come with a default automatic timestamp, and the created_at and updated_at names are created for this attribute. You do not need to worry about installing them so that the rails handle you.

+1
source

I do not have the privilege to comment on the answers to other questions, but I have a question related to him to Nerian, so we will publish this. I use code like

 before_create : create_rest def create_rest 15.times do self.players.build({:name => Rnlist.order("rand()").first.raname, :cost => 140+rand(40), :coop => rand(3)}) end end 

in the user.rb model. This code works fine and creates 15 players with different names, etc. However, if I try to put attributes in the player model as you suggest (using the create method in the players_controller controller), the self.players.build method creates 15 players with empty attributes without any errors. I thought trying to use code with a new method instead of creating in player_controller might solve this, but the result is the same. Can self.players.build method cause a problem? I checked the Ruby API for others.build (), as well as the Vinhboy.com blog , but could not solve this problem.

0
source

Source: https://habr.com/ru/post/1336149/


All Articles