NoMethodError in Contacts#new - ruby-on-rails

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.

Related

Using a Hidden Field inside a Rails 5 Partial to Save an Array

I am trying to do the seemingly innocuous task of passing an array of tags to a new subscriber form, based on which page it's rendered on.
My Subscriber table looks like this in schema.rb:
create_table "subscribers", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "phone"
t.string "email"
t.text "tags", default: [], array: true
t.text "admin_notes"
t.boolean "unsubscribe"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
My partial is rendered like this:
<%= render partial: "layouts/new_subscriber", locals: { tags: "buyer, LM-house-tour-checklist" } %>
Which brings up this _new_subscriber.html.erb partial:
<%= simple_form_for(#new_subscriber) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.label :first_name %>
<%= f.text_field :first_name, required: true, class: "form-control" %>
<%= f.label :last_name %>
<%= f.text_field :last_name, required: true, class: "form-control" %>
<%= f.label :phone %>
<%= f.text_field :phone, required: true, class: "form-control" %>
<%= f.label :email %>
<%= f.text_field :email, required: true, class: "form-control" %>
<%= f.hidden_field :tags, value: tags %>
</div>
<div class="form-actions text-center">
<%= f.submit "Get My Checklist", class: "btn" %>
</div>
<% end %>
I have tested using the Chrome code inspector that "buyer, LM-house-tour-checklist" is getting passed into the hidden field value.
However, when I submit, it creates the subscriber with a :tags attribute of ["uyer"]. This is super weird and unexplainable to me.
Some other info:
There is nothing in my subscriber model.
#new_subscriber is defined in my ApplicationController using before_action :new_subscriber and def new_subscriber #new_subscriber = Subscriber.new end
I have tried the method described here, but to no avail
Can anyone help me do this in the "Railsiest" way possible? It dosn't seem like that unusual of an ask, but I can't get it to work properly.
Maybe because it's sent as string.
Try to add attr_accessor :plain_tags in model
Instead of tags field use this attribute.
And in before_create callback write something like
before_create :populate_tags
def populate_tags
return if self.tags_plain.blank?
self.tags = self.tags_plain.split(",")
end

undefined method `first_name' for #<Profile id: nil>

Trace: https://i.gyazo.com/6487f4eee162e8c2207d7fdb5fc4ef3b.png
I can't get what's happening, I did the same process with the contact page and everything worked fine. Any ideas?
profile/new.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Create Your Profile</h1>
<p class="text-center">Be a part of the DevMatch community and fill out your profile!</p>
<div class="well">
<%= render 'form' %>
</div>
</div>
</div>
profile/_form.html.erb
<%= form_for #profile, url: user_profile_path do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :job_title %>
<%= f.select :job_title, ['Developer', 'Entrepreneur', 'Investor'], {}, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :contact_email %>
<%= f.text_field :contact_email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Update Profile", class: 'btn btn-primary' %>
</div>
<% end %>
schema
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.string "first_name"
t.string "last_name"
t.string "job_title"
t.string "phone_number"
t.string "contact_email"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Profiles controller
class ProfilesController < ApplicationController
# GET to /users/:user_id/profile/new
def new
# Render blank profile details form
#profile = Profile.new
end
end
I get the rror in the profile creator page. It says the firstname is undefined but I tried deleting it and then it says that the laast name is undefined and so on
If I delete the "<%= render 'form' %>" form the other html, the page loads perfectly, but I need a form and I am trying to learn Ruby. Sorry
undefined method `first_name' for Profile id: nil
It says the firstname is undefined but I tried deleting it and then it
says that the last name is undefined and so on
You didn't have columns in the profiles table. You should run rake db:migrate to migrate the columns which should resolve your problem.
Additionally always make sure your migrations are run properly without any errors You check the status of the pending migrations with rake db:migrate:status

nomethoderror in contacts#new making contactform in ruby

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!

How come some of my user fields get remember & pre-populated and others don't?

I am working on an app where people can go to edit their user and upon submission it redirects them to their show profile view.
This all seems to work according to plan but for some reason some when I go back to my edit view I see that some of my form fields get automatically pre-populated while others don't.
Why is this happening?
Specifically the all text fields are being remembered and pre-populated but my image file field and time weekly fields are not. They are definitely still in the database and are displayed in my show view but not pre-populated on the edit view?
Do certain types of fields not get pre-populated or what is this behavior? I would ideally like to have all of the fields pre-populated(image, dates, text, etc)
Here is my code:
Edit view:
<div class="editprofilebox">
<h1>Take a moment to fill out your profile:</h1>
<div class="container-fluid">
<div class="edit-profile-form">
<%= form_for #user, :html => { :role => 'form', :class => 'form-horizontal', :multipart => true } do |f| %>
<div class="form-inputs">
<div class="row">
<div class= "col-sm-4">
<div class: "form-group">
<%= f.text_field :first_name, class: "form-control", placeholder:"First Name" %>
</div>
<div class: "form-group">
<%= f.text_field :last_name, class: "form-control", placeholder:"Last Name" %>
</div>
<div class: "form-group">
<%= f.label :profile_image, class: "control-label" %>
<%= f.file_field :image, class: "profile-picture-upload" %>
</div>
</div>
<div class= "col-sm-4">
<div class: "form-group">
<%= f.label :twitter %>
<%= f.text_field :twitter, class: "form-control", placeholder:"Type your update title here" %>
</div>
<div class: "form-group">
<%= f.text_field :occupation, class: "form-control", placeholder:"Occupation" %>
</div>
<div class: "form-group">
<%= f.text_field :gender, class: "form-control", placeholder:"Gender" %>
</div>
<div class: "form-group">
<%= f.text_field :work_history, class: "form-control", placeholder:"Work History" %>
</div>
<div class: "form-group">
<%= f.number_field :years_of_experience, class: "form-control", placeholder:"years_of_experience" %>
</div>
</div>
<h3> time available </h3>
<div class="col-sm-2">
<%= f.label :Monday %>
<%= f.check_box :monday, class: "time-checkbox", placeholder:"Type your update title here" %>
<%= f.time_field :mondaytime1, class: "form-control time-box"%>
<%= f.time_field :mondaytime2, class: "form-control time-box" %>
<%= f.label :Tuesday %>
<%= f.check_box :tuesday, class: "time-checkbox", placeholder:"Type your update title here" %>
<%= f.time_field :tuesdaytime1, class: "form-control time-box" %>
<%= f.time_field :tuesdaytime2, class: "form-control time-box" %>
<%= f.label :Wednesday %>
<%= f.check_box :wednesday, class: "time-checkbox" %>
<%= f.time_field :wednesdaytime1, class: "form-control time-box" %>
<%= f.time_field :wednesdaytime2, class: "form-control time-box" %>
<%= f.label :Thursday %>
<%= f.check_box :thursday, class: "time-checkbox" %>
<%= f.time_field :thursdaytime1, class: "form-control time-box" %>
<%= f.time_field :thursdaytime2, class: "form-control time-box" %>
</div>
<div class:"col-sm-2">
<%= f.label :Friday %>
<%= f.check_box :friday, class: "time-checkbox" %>
<%= f.time_field :fridaytime1, class: "form-control time-box" %>
<%= f.time_field :fridaytime2, class: "form-control time-box" %>
<%= f.label :Saturday %>
<%= f.check_box :saturday, class: "time-checkbox" %>
<%= f.time_field :saturdaytime1, class: "form-control time-box" %>
<%= f.time_field :saturdaytime2, class: "form-control time-box" %>
<%= f.label :Sunday %>
<%= f.check_box :sunday, class: "time-checkbox" %>
<%= f.time_field :sundaytime1, class: "form-control time-box" %>
<%= f.time_field :sundaytime2, class: "form-control time-box" %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-default btn-lg" %>
</div>
<% end %>
</div>
</div>
</div>
show view:
<div class="profilebox">
<div class="profile-title-box" >
<%= #user.first_name %> <%= #user.last_name %> profile
</div>
<hr>
<div class="profile-container">
<div class="row ">
<div class="profile-image-box col-sm-4" >
<%= image_tag #user.image.thumb('150x185#').url if #user.image_stored? %>
</div>
<div class="profile-info-box col-sm-8">
<%= #user.email %>
</br>
<%= #user.first_name %>
</br>
<%= #user.last_name %>
</br>
<%= #user.occupation %>
</br>
<%= #user.gender %>
</br>
<%= #user.work_history %>
</br>
<%= #user.years_of_experience %>
</br>
</div>
</div>
</div>
<hr>
<div class="profile-extra-box">
<h3>Complete your profile here:</h3>
<%= link_to 'Edit', edit_user_path(#user) %>
</div>
</div>
users controller:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
end
def index
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find_by_id(params[:id])
if #user.update_attributes(user_params)
flash[:success] = "User updated successfully!"
redirect_to user_path
else
flash[:danger] = "User could not be updated!"
end
end
def destroy
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :image, :twitter, :monday, :mondaytime1, :mondaytime2, :tuesday, :tuesdaytime1, :tuesdaytime2, :wednesday, :wednesdaytime1, :wednesdaytime2, :thursday, :thursdaytime1, :thursdaytime2, :friday, :fridaytime1, :fridaytime2, :saturday, :saturdaytime1, :saturdaytime2, :sunday, :sundaytime1, :sundaytime2, :occupation, :gender, :years_of_experience, :work_history)
end
end
Scheema:
ActiveRecord::Schema.define(version: 20140906225655) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
t.string "image_uid"
t.string "image_name"
t.text "twitter"
t.boolean "monday"
t.text "mondaytime1"
t.text "mondaytime2"
t.boolean "tuesday"
t.text "tuesdaytime1"
t.text "tuesdaytime2"
t.boolean "wednesday"
t.text "wednesdaytime1"
t.text "wednesdaytime2"
t.boolean "thursday"
t.text "thursdaytime1"
t.text "thursdaytime2"
t.boolean "friday"
t.text "fridaytime1"
t.text "fridaytime2"
t.boolean "saturday"
t.text "saturdaytime1"
t.text "saturdaytime2"
t.boolean "sunday"
t.text "sundaytime1"
t.text "sundaytime2"
t.string "occupation"
t.string "gender"
t.string "work_history"
t.decimal "years_of_experience"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
"Pre-population" has two connotations, both of which you need to consider:
Browser-based data
Server-based data
Browser
The difference here is that browser based data is basically the "remember me" stuff you type into forms on e-commerce sites and the like.
The reason I mention this is because when you have a user form, modern browsers (exclusing IE) will generally populate it with the relevant data you have used before. This can be seen with this introduction to Autofill on Chrome's site:
In essence, it means that if you load standard "input names" on your pages, Chrome will endeavour to populate them with data you've either saved, or inputted into other websites.
Firstly, you need to make sure you are not having your details inputted by the browser. If this the is the case, it will mean you've got to get the server-side functionality working regardless.
Either way, you should look at using the server-based data as described below:
Server
Secondly, you'll have sever-based data. This is real Rails data, and what you need in your page:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def edit
#user = User.find params[:id]
end
end
#app/views/users/edit.html.erb
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= image_tag #user.image.url if #user.image.present? %>
<%= f.file_field :image %>
<%= f.submit %>
<% end %>
As per the form_for documentation, if you have the correct ActiveRecord object populated, and have the data in your database, calling the attribute-based input helpers should populate for you.
There are some caveats to this, however:
--
Image
Using file_field will not pre-populate your image.
The file upload element is distinctly different to the image element - simply that the upload element cannot show you an image. This means you have to explicitly show the image in your edit form, if indeed you want to show it:
# Edit View
<%= image_tag #user.image.thumb('150x185#').url if #user.image_stored? %>
<%= f.file_field :image %>
We've used this method here (just sign up for free and try to upload a profile image):
Although we used JQuery heavily here, we made it so that the image form shows the image, which then gives you the ability to upload a new one.
--
Time
Frankly, I'm not sure about your time field.
Like the explanation above, you'll want to ensure you're using the attributes from your database to populate your time fields. I see you're using a lot of different checkboxes, which although might help create a better system, will likely not populate the data you want

How to have two different text outputs w/ ruby on rails 4

Hi I'm using text forms while using pins. My code below:
_form.html.erb
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
Index.html.erb
<div class="center">
<h1><strong><%= pin.description %></strong></h1>
<h3><%= pin.description %></h3></div>
<%= link_to image_tag(pin.image.url(:medium)), pin %>
show.html.erb
<div class="panel-heading center">
<%= image_tag #pin.image.url(:medium) %>
</div>
<div class="panel-body">
<h1><strong><%= #pin.description %></strong></h1>
<h3><%= #pin.description %></h3>
I get the same text output when I create a pin and descriptions using that code. I've tried changing the :description method into something else but then I got an error saying I can't load the page. How can I solve this problem? Thank You
ActiveRecord::Schema.define(version: 20131017222026) do
create_table "pins", force: true do |t|
t.string "description"
t.string "definition"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
I'm guessing this is your problem (your question really lacks context):
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
If you try and output fields on a form, you'll have to output the fields relating to your model's schema. For example, if you just type f.text_field :my_name, you'll get a no method error
As you've shown your schema, you could put a text field for any of the attributes your record has, such as:
<div class="form-group">
<%= f.label :definition %>
<%= f.text_field :definition, class: "form-control" %>
</div>
No Method Errors
A quick note on no method errors for you
Since Ruby on Rails is Object Orientated, everything you do with it has to involve an object. The best example of this is ActiveRecord objects - they are a variable populated with table data
The reason why you see no method errors is simply because you're trying to perform an action or method on an object which does not support it
So if you have #companies, and try to perform #companies.is_my_name_richard? - it will likely come back with a no method error. The way around this is to ensure you only call the model's attributes (if you're outputting the variable), or only use functions which have been defined in the controller or model

Resources