How to add a variable to an existing ruby ​​on rails model

Very green question. I created a simple blog following the instructions here http://guides.rubyonrails.org/getting_started.html

How to add a new string variable to a post object?


As soon as I have a new variable, how do I create new entries in html.erb files? The code below gives me a NoMethodError exception for the email method. How to make this code error free?

btw - what is the stackoverflow convention for subsequent questions?

<h2>Add a post:</h2> <%= form_for([@post, @post.actions.build]) do |f| %> <div class="field"> <%= f.label :number_performed %><br /> <%= f.text_field :number %> </div> <div class="field"> <%= f.label :your_email %><br /> <%= f.text_field :email %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 
+4
source share
3 answers

At least in order to get minimal functionality, you should add another column to your message table.

See here how to add a column program:

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

OR you can run the rail generation command like this:

 rails generate migration AddColumnNameToPost column_name:string 

Regardless of which route you go down, make sure that to use them in the database, do the following:

 rake db:migrate 

From there you can access:

 @post = Post.new @post.column_name = "value" #etc 
+17
source

same answer from drharris :

 rails generate migration add_newvariableone_and_newvariabletwo_to_modelpluralname newvariableone:string newvariabletwo:string 

it will create a ruby ​​file inside db / migrate where the contents are of type

 class AddNewVariableOneAndNewVariableTwoToModelPluralname < ActiveRecord::Migration def self.up add_column :modelpluralname, :newvariableone, :string add_column :modelpluralname, :newvariabletwo, :string end def self.down remove_column :modelpluralname, :newvariableone remove_column :modelpluralname, :newvariableone end end 

hope this helps you thank

+5
source

You should look at the Migrations section. In your case, you should use the command:

 rails generate migration AddRandomStringToPost random_string:string 
0
source

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


All Articles