Probably very simple. Ruby on rails error when using web interface - ruby-on-rails

I am following my school's ruby on rails tutorial. I have followed the instructions very closely, but I have come across an error. My web interface works fine when I do localhost:3000/books (this results in me seeing my database of my book entries, which I added using the Firefox SQL manager). However, when I click "New Book" to make a new entry through the web interface, I get:
SyntaxError in BooksController#new
(NOTE: when I do localhost:3000/publisher/new, it works fine. When I do localhost:3000/authors/new, it doesn't work. I'm hoping the issue with the Author will be resolved when I fix the issue with the Book. All of the following information is about the books part of my project.)
c:/Sites/Summer_2014/BookManager/app/models/author.rb:4: syntax error, unexpected ':', expecting keyword_end validates_presence_of : :first_name, :last_name ^
This is the app trace:
app/views/books/_authors_checkboxes.html.erb:2:in `_app_views_books__authors_checkboxes_html_erb__770955296_43280532'
app/views/books/_form.html.erb:26:in `block in _app_views_books__form_html_erb___935905997_43301772'
app/views/books/_form.html.erb:1:in `_app_views_books__form_html_erb___935905997_43301772'
app/views/books/new.html.erb:3:in `_app_views_books_new_html_erb___378444391_43261320'
Here are the parts of code that I think are relevant:
app/view/books/_authors_checkboxes.html.erb:
<p>
<% for author in Author.alphabetical %>
<%= check_box_tag "book[author_ids][]", author.id, #book.authors.include?(author) %>
<%= author.name %>
<br />
<% end %>
</p>
app/views/books/_form.html.erb:
<%= form_for(#book) do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :year_published %><br>
<%= f.number_field :year_published %>
</div>
<div class="field">
<%= f.label :publisher_id %><br>
<%= f.collection_select :publisher_id, Publisher.alphabetical, :id, :name, :prompt => 'Please select a publisher' %>
</div>
<%= render partial: 'authors_checkboxes' %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
app/views/books/new.html.erb:
<h1>New book</h1>
<%= render 'form' %>
<%= link_to 'Back', books_path %>
EDIT: Here is my apps/models/author.rb:
class Author < ActiveRecord::Base
has_many :book_authors
has_many :books, through: :book_authors
validates_presence_of : :first_name, :last_name
scope :alphabetical, -> {order('last_name, first_name')}
def name
"#{last_name}, #{first_name}"
end
end

The ruby compiler is exceptionally clear on this one, it did not expect your solitary : in author.rb on line 4. It is clearly not valid ruby and you should follow the docs for validates_presence_of. The line should simply say
validates_presence_of :first_name, :last_name

Related

Nested Resources in show template

I'm having trouble understanding nested resources. There is a Service scaffold, and there is Symptom model. I wanted to be able to add the symptom_item string to the service scaffold.
This is my current show template code:
<% if #service.symptoms.any? %>
<% #service.symptoms.each do |symptom| %>
<li><%= symptom.symptom_item %></li>
<% end %>
<% else %>
<p>
none
</p>
<% end %>
Here is my form code:
<%= form_for(#service) do |f| %>
<% if #service.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#service.errors.count, "error") %> prohibited this service from being saved:</h2>
<ul>
<% #service.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :s_text %><br>
<%= f.text_area :s_text %>
</div>
<div class="field">
<%= f.label :img_text %><br>
<%= f.text_field :img_text %>
</div>
<%= f.fields_for :symptoms do |builder| %>
<div class="field">
<%= builder.label :symptom_item %><br>
<%= builder.text_field :symptom_item, :rows => 3 %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is my controller code that allow the symptom_attributes:
def service_params
params.require(:service).permit(:name, :s_text, :img_text, symptom_attributes: [:id, :service_item])
end
I know the problem is in the show template. When I write something as simple as:
= #service.symptoms
it gives me something funky like this:
#<Symptom::ActiveRecord_Associations_CollectionProxy:0x000000095295c8>
Does anyone see what's wrong with my code in the show template or maybe some where else?
Here are my models just in case:
class Service < ActiveRecord::Base
has_many :symptoms
accepts_nested_attributes_for :symptoms, allow_destroy: true
end
class Symptom < ActiveRecord::Base
belongs_to :service
end
It should be symptoms_attributes instead of symptom_attributes and symptom_item instead of service_item in your strong parameters
def service_params
params.require(:service).permit(:name, :s_text, :img_text, symptoms_attributes: [:id, :symptom_item])
end

Nested forms rails field does not update

I am trying to do nested forms like mentioned here.
http://guides.rubyonrails.org/form_helpers.html#nested-forms
The goal is as follows: I have multiple colli with one checkbox which can be checked. The colli list can be deleted or modified but the checks and their information need to stay.
Model
class Colli < ActiveRecord::Base
has_one :check, foreign_key: "subcontainerid", primary_key: "colliid"
accepts_nested_attributes_for :check, allow_destroy: true
end
class Check < ActiveRecord::Base
belongs_to :colli
end
So every colli has one check. The colliid from the colli table created a link with the check table using the subcontainer id.
Controller
Within the colli controller I whitelist the check_attributes.
def colli_params
params.require(:colli).permit(:colliid, :collinaam, check_attributes: [:id, :checked])
end
Form
My form looks like this.
<%= form_for(#colli) do |f| %>
<% if #colli.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#colli.errors.count, "error") %> prohibited this colli from being saved:</h2>
<ul>
<% #colli.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :checks do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% end %>
<div class="field">
<%= f.label :colliid %><br>
<%= f.text_field :colliid %>
</div>
<div class="field">
<%= f.label :collinaam %><br>
<%= f.text_field :collinaam %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If I do form_for :check I can't see the checkboxes. When I do form_for :checks I see a checkbox but it does not work. When clicking submit I see following error:
undefined method `checked' for nil:NilClass
<p>
<strong>Checked:</strong>
<%= #colli.check.checked %>
</p><p>
<strong>Collinaam:</strong>
<%= #colli.collinaam %>
Which means it did not get saved. Does anybody know how to fix this?
Try adding this to your form-
<%= f.fields_for :checks, #colli.check.build do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% end %>

Date is not working in rails application with mongo id

I have create an rails application with mongoid but im facing one problem at DATE
I created an scaffold with a name "posting"
When im editing date it will updated....
I follow the instruction of Railscast #238 Mongoid here
there is my posting.rb file
class Posting
include Mongoid::Document
field :title
field :description
field :comments
field :published, :type => Date
end
this my _from.html.erb
<%= form_for(#posting) do |f| %>
<% if #posting.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posting.errors.count, "error") %> prohibited this posting from being saved:</h2>
<ul>
<% #posting.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<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 :published %><br>
<%= f.date_select :published %>
</div>
<div class="field">
<%= f.label :comments %><br>
<%= f.text_area :comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and finally my show.html.erb file
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #posting.title %>
</p>
<p>
<strong>Description:</strong>
<%= #posting.description %>
</p>
<p>
<strong>published:</strong>
<%= #posting.published %>
</p>
<p>
<strong>Comments:</strong>
<%= #posting.comments %>
</p>
<%= link_to 'Edit', edit_posting_path(#posting) %> |
<%= link_to 'Back', postings_path %>
What do you mean by not working? doesn't look like you have used published property in any of your views.
in your show.html.erb you are using
<%= f.date_select :pub %>
and in your show.html.erb you are using
<%= #posting.pub %>
However there is no property called pub in your Posting model. What you have there is called published
field :published, :type => Date
You either need to rename it in the model, or in the views to match.

Rails has many through not working

I've got 3 models:
Vacancy
has_many :address_vacancies
has_many :addresses, through: :address_vacancies
Address
has_many :address_vacancies
has_many :vacancies, through: :address_vacancies
AddressVacancy
belongs_to :address
belongs_to :vacancy
and in my form I use the following code:
<%= f.collection_select :address_id, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
which throws an error: undefined method address_id', why is that and what am I doing wrong?
Edit
The full form looks like this:
<%= form_for(#vacancy) do |f| %>
<% if #vacancy.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#vacancy.errors.count, "error") %> prohibited this vacancy from being saved:</h2>
<ul>
<% #vacancy.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.collection_select :address, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
</div>
<div class="field">
<%= f.label :signup_until %><br>
<%= f.date_select :signup_until %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This:
<div class="field">
<%= f.label :address %>
<%= f.collection_select :address, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
</div>
Isn't valid in the context of a Vacancy form (from the form_for #vacancy) since a Vacancy doesn't have a single address attribute. It has a collection of addresses through the has_many association. It would also be invalid to use :address_id for the same reason.
If you want to edit the various addresses for the one vacancy, you would need a subform. You'd use accepts_nested_attributes_for in the model, and fields_for in the view.
<%= form_for #vacancy do |f| %>
...
<%= f.fields_for :addresses do |af| %>
...
<!-- Here you'd render a partial or a set of inputs for this address %>
<%= af.input :street %> <!-- e.g., if there's a street attribute for Address -->
...
<% end %>
...
<% end %>

Ruby Rails form

Hi there I'm extremely new to Ruby and I'm currently working with two models and two corresponding databases. A Quiz which has_many questions. And I'm trying to get the questions to show up in the quiz "edit view" here's what I have.
What currently is not working is the form entry for "questions" (see commented part below). I don't think I understand the expressions preceded by the colons very well. (e.g. :title, :quiz_date etc>) are they regular variables?
Anyway, the block of code in question successfully makes a form but using :questions it puts all the information from the questions row of the database into to the form filed (i.e id, question, answer, possible answers, etc). But it doesn't give me just the value of the question field.
But if I change it to :question (hoping to just get the value of the question field in the questions table) I get an error. I also tried :questions.question and just plain questions.question. None of these worked.
Any suggestions?
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved: </h2>
<ul>
<% #quiz.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :quiz_date %><br />
<%= f.date_select :quiz_date %>
</div>
<div class="field">
<%= f.label :reading %><br />
<%= f.text_field :reading %>
</div>
<div>
#the problem code
<% #quiz.questions.each do |questions| %>
<div class="question">
<%= f.label :questions %><br />
<%= f.text_field %>
</div>
<% end %>
#end problem code
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You are going to need to adds accepts_nested_attributes in your Quiz model
Class Quiz < ActiveRecord::Base
accepts_nested_attributes_for :questions
Then in your form add
<%= f.fields_for :questions do |build| %>
<div class="question">
<%= build.label :questions %><br />
<%= build.text_field :questions %> #whatever attribute in your questions model
<%end%>
You will find this Railscast very helpful, http://railscasts.com/episodes/196-nested-model-form-part-1

Resources