nomethoderror in contacts#new making contactform in ruby - ruby-on-rails

I am new to ruby on rails and i am currently stuck trying to create a contact form.
I get the report saying : undefined method `name' for #, but in the i did create the method for name :
create_table :contacts do |t|
t.string :name
I'v tried to fix this error so i can preview the page, but i keep getting an error. I hope one of you can help me, thanks in advance!
NoMethodError in Contacts#new
Showing /home/nitrous/workspace/simplecodecast_saas/app/views/contacts/new.html.erb where line #7 raised:
undefined method `name' for #<Contact id: nil>
Extracted source (around line #7):
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %> <---- (This is line 7)
</div>
<div class="form-group">
Rails.root: /home/nitrous/workspace/simplecodecast_saas
Application Trace | Framework Trace | Full Trace
app/views/contacts/new.html.erb:7:in `block in _app_views_contacts_new_html_erb__2291340040590759835_34535240'
app/views/contacts/new.html.erb:4:in `_app_views_contacts_new_html_erb__2291340040590759835_34535240'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
My routes.rb code:
Rails.application.routes.draw do
resources :contacts
get '/about' => 'pages#about'
root 'pages#home'
and my contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
end
end
I added this to my model: contact.rb
class Contact < ActiveRecord::Base
end
And the db file:
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
and last my html page
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<% f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>

I found a fix.
I ran the rake db:rollback cmd and then the rake db:migrate.
Now it is working.
Sorry for wasting your time :) Hopefully someone else can benifit from this answer!

Related

Rails error message on views folder

Ive been going through this for hours and i cant seem to find the problem. Im doing a simple contacts form in rails , this is a tutorial im following so bear with me. I get the following error message :
undefined method `name' for #
I have this in my DB/migrate file:
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
this on my contacts_controller.rb file:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
end
end
and this is on my pages_controller.rb:
class PagesController < ApplicationController
def homepage
end
def about
end
end
This is my html :
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
Did you run your migration?
If not, do so:
bundle exec rake db:migrate
This will create the contacts table in your database (which has the name column along with other columns email and comments that you're using while building your form). Also, re-start your rails server and then you should be able to use your form code without any problem.
You have probably not migrated your table.
In your console run:
rake db:migrate
Then restart your server and try again.
The other answers summed it up well - you need to migrate your contacts table:
$ rake db:migrate
You'll want to read up on Rails migrations here.
As an aside,
Pages
I don't know which tutorial everyone seems to be following, it's telling you to use specific actions for static pages, which IMO is pretty bad practice (especially in the long run).
A much more versatile (and "Railsy*) solution is to put the content for the data in the database, which you can then call with a model - allowing you to have a single show view for all your pages:
#config/routes.rb
resources :pages, only: [:show]
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def show
#page = Page.find params[:id]
end
end
#app/views/pages/show.html.erb
<%= #page.title %>
<%= #page.body %>
You'd have to create another migration for the pages datatable, as follows:
$ rails g migration CreatePages
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
$ rake db:migrate
HTML
Your current HTML can be a lot more efficient:
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for #contact do |f| %>
<% attributes = {name: "text_field", email: "email_field", comments: "text_area"} %>
<% attributes.each do |attr,val| %>
<div class="form-group">
<%= f.label attr.to_sym %>
<%= f.send(val, attr.to_sym, class: 'form-control') %>
</div>
<% end %>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>

NoMethodError in Contacts#new

So I'm taking this course on udemy and working through the app I get stuck here! I've rewatched the vids and corrected all I saw. Can someone help me get past this error?
NoMethodError in Contacts#new
Showing /home/ubuntu/workspace/app/views/contacts/new.html.erb where line #17 raised:
undefined method `comments' for #<Contact:0x007f490a778008>
Extracted source (around line #17):
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
The rails model Contact does not seem to have the attribute comments, as per your comment this is your current model:
schema.rb:
ActiveRecord::Schema.define(version: 20150905160857) do
create_table "contacts", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "text"
t.datetime "created_at"
t.datetime "updated_at"
end
end
you would have two options:
Option 1:
instead of using comments, use text in your view:
<%= f.label :text %>
<%= f.text_area :text, class: 'form-control' %>
Option 2:
You could create a new migration to adjust your model renaming the column text or adding the column comments to your model.
Execute this from the rails app folder:
rails g migration adjustingContact
navigate to the folder /your-app-folder/db/migrate and edit the file <datestring>_adjustingContact.rb adding or renaming your comments column
execute a migration:
rake db:migrate
I hope this helps.

Undefined method for nil

I'm trying to finish the codecademy ruby on rails track. I'm getting this error:
Showing /home/ccuser/workspace/learn-rails_innovation-cloud/innovation
cloud/app/views/signups/new.html.erb where line #41 raised:
undefined method `content' for #<Signup id: nil, email: nil, created_at: nil, updated_at: nil>
Extracted source (around line #41):
<%= form_for(#signup) do |f| %>
<div class="field">
<%= f.label :message %><br>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Create" %>
Here's my controller:
class SignupsController < ApplicationController
def new
#signup = Signup.new
end
end
And finally my migration file:
class CreateSignups < ActiveRecord::Migration
def change
create_table :signups do |t|
t.text :email
t.timestamps
end
end
end
Any insight is appreciated to understand what I'm doing wrong.
content field is missing in your signups table. Please add using migration.
Your form code should look like:
<%= form_for(#signup) do |f| %>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Create" %>
And it should work now.

Could not find table 'projects'

I am getting the following error message on my localhost:3000/project/new
Could not find table 'projects'
Extracted source (around line #8):
#project = Project.new(params[:project]) --- line8
I am rendering a table from a partial in my projects folder
<h1>This is a form</h1>
<%= form_for(#project) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :plan %><br />
<%= f.text_field :plan %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
to the new file/action in the projects folder
<h1>Submit a new project here</h1>
<%= render 'form' %>
This is my Project Model
class Project < ActiveRecord::Base
belongs_to :user
validates :title, :uniqueness => true
end
This is my Routes
dsfasfd::Application.routes.draw do
resources :project
devise_for :users
root to: "home#index"
I ran rake:db migrate but its not doing anything, not creating a db and I can't find anything in the schema about projects either. Can't figure out what I'm doing wrong, why won't my rake create a table?
Firstly generate a migration file.
rails g migration products title:string description:string plan:string
and then migrate it
rake db:migrate
Follow this Guide.You really need this.
Update:
Seems like you have generated an empty migration file.Now have to do
1.Open that migration file and add these lines under the def
t.string :title
t.string :description
t.string :plan
2.Do rake db:migrate after that.

undefined method `merge' for nil:NilClass in a form using text_field_tag

I'm following this tutorial to create tags for a model (in my case the model Post):
controllers/posts_controller.rb:
def create
#user = current_user
#post = #user.posts.new(params[:post])
if #post.save
redirect_to #post, notice: 'post was successfully created.'
else
render action: "new"
end
#post.tag!(params[:tags])
end
views/posts/_form.html.erb:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :tags %>
<%= f.text_field :tags, params[:tags] %>
</div>
<div class="actions">
<%= f.submit %>
</div>
views/posts/show.hmtl.erb:
<div class="tags">
<h4>Tags:</h4>
<%= render #post.tags %>
</div>
models/post.rb:
class Post < ActiveRecord::Base
has_and_belongs_to_many :tags
def tag!(tags)
tags = tags.split(" ").map do |tag|
Tag.find_or_create_by_name(tag)
end
self.tags << tags
end
end
models/tag.rb:
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end
db/migrate/(etc...)_create_tags.rb:
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
end
create_table :tags_posts, :id => false do | t |
t.integer :tag_id, :post_id
end
end
end
Now, when I visit the posts form I get this error:
undefined method `merge' for nil:NilClass
Extracted source (around line #13):
10: </div>
11: <div class="field">
12: <%= f.label :tags %>
13: <%= f.text_field :tags, params[:tags] %>
14: </div>
15: <div class="actions">
16: <%= f.submit %>
When I visit a post I get this error:
SQLite3::SQLException: no such table: posts_tags: SELECT "tags".* FROM "tags" INNER JOIN "posts_tags" ON "tags"."id" = "posts_tags"."tag_id" WHERE "posts_tags"."post_id" = 7
Extracted source (around line #24):
21:
22: <div class="tags">
23: <h4>Tags:</h4>
24: <%= render #post.tags %>
25: </div>
26:
27: </div>
But I do have these tables as you can see in my schema.rb file:
create_table "tags", :force => true do |t|
t.string "name"
end
create_table "tags_posts", :id => false, :force => true do |t|
t.integer "tag_id"
t.integer "post_id"
end
Any suggestions to fix this?
Just in case anyone else comes across this old question through google:
You need to use 'value:' for the default value in a simple form.
<%= f.text_field :tags, value: params[:tags] %>
You have the table name backwards. HABTM looks for the models alphabetically. Look at the error carefully. It says posts_tags cannot be found. You create tags_posts. So change your table name to posts_tags.
try this <%= f.text_field_tag :tags, params[:tags] %> in your partial _form.html.erb.
<%= form_with(
url: order_register_path(session[:order_id]),
method: :put
) do |form| %>
<%= form.check_box :review_budget %>
<%= form.check_box :fast_delivery %>
<%= form.check_box :replace_similar %>
<div>
<%= f.submit "Continuar" %>
</div>
<% end %>
f.submit instead of that submit_tag

Resources