Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How can I add another string variable to the post object?


Once I have a new variable, how do I create new posts in html.erb files? The code below gives me a NoMethodError exception for the 'email' method. How do I make this code run without an error?

btw - what is convention on stackoverflow for followup 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 %>
like image 746
will Avatar asked Nov 29 '22 04:11

will


1 Answers

At the very least to get the minimum functionality, you must add another column to your post table.

See here on how to add a column programitcally:

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

OR you can run the rails generate migration command like so:

rails generate migration AddColumnNameToPost column_name:string

No matter what route you go down, make sure you run the following to apply those migrations to your database:

rake db:migrate

From there you can access:

@post = Post.new
@post.column_name = "value"
#etc
like image 56
Mike Lewis Avatar answered Dec 06 '22 23:12

Mike Lewis