Could not find table 'projects' - ruby-on-rails

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.

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 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!

Ruby on Rails, two models in one form

I have two very similar models Pretreatment and Diagnosis, that belong to the model Patient:
class Pretreatment < ActiveRecord::Base
belongs_to :patient
attr_accessible :content
end
class Diagnosis < ActiveRecord::Base
belongs_to :patient
attr_accessible :content
end
class Patient < ActiveRecord::Base
attr_accessible :age, :name, :city, :street, :number
has_many :anamneses
has_many :befunds
end
On the Patient show page I'm displaying two forms, one for the Preatreatment and another for the Diagnosis:
<%= form_for([#patient, #patient.preatreatments.build]) do |f| %>
<div class="field">
<%= f.label :conten %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_for([#patient, #patient.diagnosiss.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My question is how can I bring the two forms together, so that the user only has to press once the submit button? Im not sure but I think nested attributes is not the right thing to handle it, maybe thefields_for` tag?
Update I tried to use fields_for tag:
<%= form_for([#patient, #patient.pretreatment.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<%= fields_for([#patient, #patient.diagnosiss.build]) do |u| %>
<div class="field">
<%= u.label :content %><br />
<%= u.text_field :content %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But I get the error:
undefined method `model_name' for Array:Class in <%= fields_for([#patient,#patient.befunds.build]) do |u| %>
Use fields_for for the associated models.
There should be no square brackets arround the parameters of fields_for
In your code example, I cannot find the relation between Patient and Diagnosis, and the plural of diagnosis is diagnoses, you can specify this in config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'diagnosis','diagnoses'
end
So your Patient model should contain
class Patient < ActiveRecord::Base
attr_accessible :age, :name, :city, :street, :number
has_many :diagnoses
end
And you can write in your form:
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<%= fields_for(#patient, #patient.diagnoses.build) do |u| %>
<div class="field">
<%= u.label :content %><br />
<%= u.text_field :content %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
You can achieve that using nested attributes :
patient.rb
class Patient < ActiveRecord::Base
attr_accessible :age, :name, :pretreatments_attributes, :diagnosiss_attributes
has_many :pretreatments
has_many :diagnosiss
accepts_nested_attributes_for :pretreatments
accepts_nested_attributes_for :diagnosiss
end
patients_controller.rb
def show
#patient = Patient.find(params[:id])
#patient.pretreatments.build
#patient.diagnosiss.build
respond_to do |format|
format.html # show.html.erb
format.json { render json: #patient }
end
end
patients/show.html.erb:
<%= form_for #patient do |f|%>
<h3>Pretreatments:</h3>
<%= f.fields_for :pretreatments do |field| %>
<%= field.label "Content" %></div>
<%= field.text_field :content %>
<% end %>
<h3>Diagnosis:</h3>
<%= f.fields_for :diagnosiss do |field| %>
<%= field.label "Content" %></div>
<%= field.text_field :content %>
<% end %>
<%=f.submit %>
<% end %>
And that all
There are a few ways of doing this:
The way the fields_for works is you have a form_for for the parent model and within it you can place fields_for the models which belong to the parent. A good example is given in the Rails Docs
A simple and more extensible option over time but not very "semantic" one would be to use JavaScript/JQuery. You could trigger $("form #new_pretreatments").submit(); and the same for the Diagnosis once a button is clicked.
Maybe instead you could have just one model to store them both. For example a model called Disease. Each time a patient gets a disease a new one is created and it has columns for each the Diagnosis and Pretreatment. It's probably the best option instead of adding another model for each bit of info a patient can have.
You can use fields_for for the second model, which works like form_for but doesn't generate the form tags. See the docs.
There are some gems available for nested forms. one of them is awesome_nested_fields. I haven't used this earlier but that shows good code in documentation. Another one is simple_form.
Hope that helps!!!

Ruby on Rails - Association issue :(

I'm a newbie and have searched many posts and railscast tutorials and still cant get around this associations thing.
I have 2 models, a Hotel (created by scaffolding) and Facility.
The point is to associate a facility to a hotel, but since my facility table has all the columns it needs with boolean type), a row in that table would do for one hotel.
The thing is, I can't get it to show, and save/edit/update de facilities. I've created a hotel_id colum when creating the model Facility.
My code is:
Models:
class Facility < ActiveRecord::Base
belongs_to :hotel
attr_accessible :concierge, :hotel_id, :room24h
end
class Hotel < ActiveRecord::Base
has_one :facility, :dependent => :destroy
accepts_nested_attributes_for :facility, :allow_destroy => true
attr_accessible :name, :rating, :recommended, :facility_attributes
end
My form in the view is:
<%= form_for(#hotel) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :rating %><br />
<%= f.number_field :rating %>
</div>
<div class="field">
<%= f.label :recommended %><br />
<%= f.check_box :recommended %>
</div>
<br />
Hotel Facilities
<%= f.fields_for :facility do |facility_fields| %>
<div class="field">
<%= facility_fields.label :room24h, "24h Room Service:" %>
<%= facility_fields.check_box :room24h %>
</div>
<div class="field">
<%= facility_fields.label "Concierge:" %>
<%= facility_fields.check_box :concierge %>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<%end%>
As for Controllers, the hotels_controller is the same as if you had just done scaffolding and my facilities_controller is empty.
It is now showing the facilities in the form, but when i click "Create" and it submits, I get:
"Can't mass-assign protected attributes: #hotel"
and
app/controllers/hotels_controller.rb:46:in `new'
app/controllers/hotels_controller.rb:46:in `create'
as for parameters input:
{"hotel"=>{"rating"=>"1",
"name"=>"aaa",
"recommended"=>"0",
"#hotel"=>{"room24h"=>"1",
"concierge"=>"1"}},
"commit"=>"Create Hotel",
"utf8"=>"✓",
"authenticity_token"=>"YU7KEJ8qz0iQcXPGkLP6BSJn7JL6df1HvuS5JnjK2eU="}
Any ideas? What is missing in the controllers? Thanks in advance again
In New/Edit action create #hotel.facility || #hotel.build_facility
Problem solved. To anyone who has the same issue: Added to the new controller:
#facility=#hotel.build_facility
#klump, that syntax was incorrect, after trying the correct one is:
<%= f.fields_for :facility do |fc|%>
So thank you Amar, for pointing me in the right direction.

Creating forms for Multiple Nested Resources in Rails 3

I have been following Ryan Bates' tutorial on nested forms Railscast 196
The form for the new action shows the nested attributes for quizzes but does not show nested attributes for the key. I am guessing this is because quizzes have a has_many relationship where key has a has_one relationship... But I cannot figure out what I'm doing wrong?
Any help is much appreciated!
This is my model:
class Repository < ActiveRecord::Base
has_many :quizzes, :dependent => :destroy
has_one :key, :dependent => :destroy
accepts_nested_attributes_for :key, :quizzes
end
This is my controller:
def new
#repository = Repository.new
3.times { #repository.quizzes.build }
#repository.key = Key.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #repository }
end
end
This is my view:
<div class="field">
<%= f.label :wp_uid %><br />
<%= f.text_field :wp_uid %>
<% f.fields_for :quizzes do |quiz_fields| %>
<p>
<%= quiz_fields.label :name, "Name" %><br />
<%= quiz_fields.text_field :name %>
</p>
<% end %>
<% f.fields_for :key do |key_fields| %>
<div class="field">
<%= key_fields.label :value, "Value" %><br />
<%= key_fields.text_field :value %>
</div>
<div class="field">
<%= key_fields.label :expiry, "Expiry" %><br />
<%= key_fields.date_select :expiry %>
</div>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should try modifying your fields_for blocks to use <%= %>
Try changing to:
<%= f.fields_for :key do |key_fields| %>
The railscast could have been made before the change in Rails 3 to use <%= %> instead of <%%>.
Ryan has a nested_form gem that you may find useful for this as well. I haven't tried using it yet, but plan to next time I start a new project.
https://github.com/ryanb/nested_form
Try building the key object as
#reposity.build_key
From the rails docmentation
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Unsaved+objects+and+associations
If you wish to assign an object to a has_one association without saving it, use the build_association method. The object being replaced will still be saved to update its foreign key.

Resources