Where are the errors displayed? restrict_with_error validation - ruby-on-rails

I'm looking to use the restrict_with_error validation in Rails 4.2.
In this application, an area has multiple apartments. I want to prevent area from being deleted if it has apartments associated with it.
Here is the area model:
class Area < ActiveRecord::Base
has_many :apartments, :dependent => :restrict_with_error
validates :name, presence: true
end
Here is the view where the user can delete an area:
<% #areas.each do |a| %>
<tr>
<td><%= a.id %></td>
<td><%= a.name %></td>
<td><%= a.notes %></td>
<td><%= link_to 'Destroy', a, method: :delete, data: { confirm: 'Are you sure you want to delete this area?' } %></td>
</tr>
<% end %>
</tbody>
</table>
If I try to delete an area that has associated apartments, it is restricted. However, no error is displayed.
This might be a very simple question, but where is the error displayed if the deletion is restricted?
Thanks so much in advance!

You need to access the area errors. You can do so in the area controller like this:
def destroy
unless #area.destroy
flash[:notice] = #area.errors.full_messages[0]
end
redirect_to areas_path
end

Related

How can i get User name in a table where user_id is used as foreign key

I am using Devise gem for user Authentication. User id is as foreign key in article table
How can i get writer name through User_id in a view Show_article.html.erb
I can access user_id in show_article.htmlerb
I have tried to make a custom function in article controller but could not get the desired output
your Article model should look like this:
class Article << ActiveRecord::Base
belongs_to :user
#....some more lines
end
your User model should look like this:
class User << ActiveRecord::Base
has_many :articles
#....some more lines
end
and your show.html.erb should say:
#....your rest of code
<%= #article.try(:user).try(:name) %>
#....your rest of code
This will skip user name if you article doesn't have any user. It looks like your article doesn't have user_id or you haven't defined your relations correctly.
It might be the case that some of your article do not have any user. so
you can do
<%= #article&.user&.name %> in show page
and in the index page
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article&.user&.name%></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure you want to delete this article?' } %></td>
</tr>
<% end %>
but make sure you have ruby 2.3.0 or higher

Create a child resource through a parent resource's index table - Ruby on Rails

Hi I want to put a link in a index table of a resource to create a child resource.
I got a Sale, and an operation:
class Sale < ApplicationRecord
belongs_to :user
has_one :operation, dependent: :destroy
end
class Operation < ApplicationRecord
belongs_to :sale
end
And I want to be able to create an operation through an index table of sales.
Thing is, I need it to be created on the button/link click, and not be transferred to a new operation view. (Operation has only sale_id and id as attributes)
Something like that
<table>
<tr>
<th>Sale Id</th>
</tr>
<% #sales.each do |sale| %>
<td><%= sale.id %></td>
<td><%= link_to 'Show', sale_path(sale) %></td>
<td><%= link_to 'Edit', edit_sale_path(sale) %></td>
<td><%= link_to 'Delete', sale,
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<td> <%= form_for (#operation) do |o| %>
<%= o.submit 'Create Operation'%>
<% end %>
</td>
</tr>
<% end %>
</table>
But I got this error: 'First argument in form cannot contain nil or be empty'
I also tried this:
<td>
<%= fields_for :operation, #sales.operation do |o| %>
<%= o.submit "Create Operation" %>
<% end %>
</td>
And I got this:
undefined method `operation' for Sale::ActiveRecord_Relation:0xb1a4ece4>
tried this as well :
<td>
<%= link_to 'Create Operation', 'operations#create' %>
</td>
But it sends me to the operation index view without creating an operation.
First argument in form cannot contain nil or be empty
The error means #operation is nil in the form. You should define #operation in the sales#index method as the form is in sales/index.html.erb
def index
#operation = Operation.new
end
I need it to be created on the button/link click, and not be
transferred to a new operation view
If you meant,you shouldn't be redirected to another view after the button, then you have to use AJAX. I recommend you see these Guides to understand how to do with AJAX

How to update multiple records with rails 4 & mongoid

I want to model student's attendance, i am following railscast.com/165-edit-multiple-revised i follow Ryan's first approach and try to use it with mongoid. My modified code is as follows but it does not update multple records, well it even doesnt update any of the record. sounds like mongoid does not found any Student with id of an array. plz see the code and let me know what i am doing wrong.
# config/routes.rb
resources :students do
collection do
put :attendance
end
end
# model/student.rb
class Student
include Mongoid::Document
field :name, type: String
field :present, type: Mongoid::Boolean
end
# controllers/students_controller.rb
def attendance
Student.where(id: params[:student_ids]).update_all(present: true)
redirect_to students_url
end
# views/students/index.html.erb
<% #students.each do |student| %>
<tr>
<td><%= check_box_tag "student_ids[]" , student.id %></td>
<td><%= student.name %></td>
<td><%= student.present %></td>
<td><%= link_to 'Show', student %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
if i change the code in controller as follows than it works but i am not satisfied with this code. is there any other way around or i am doing something wrong?
# controllers/students_controller.rb
def attendance
params[:student_ids].each do |student_id|
Student.where(id: student_id).update_all(present: true)
end
redirect_to students_url
end
If you want to fetch a set of student, you should try the in operator. Try this:
def attendance
Student.where(:id.in => params[:student_ids]).update_all(present: true)
redirect_to students_url
end
What do you think?

email Model and email column clashing in Rails

I have a Submitter Model which has an email column. I also have an Email Model that has email suffix column in it.
My issue (I believe) is that since my Submitter Model has an email column, when I try to use submitter.email.suffix to display the suffix in my Email model it says there's no defined method.
Any idea what I can do here?
View:
<% #submitters.each do |submitter| %> <tr>
<td><%= submitter.school.name %></td>
<td><%= submitter.first_name %></td>
<td><%= submitter.last_name %></td>
<td><%= submitter.email %><%= submitter.email.suffix %></td>
<td><%= link_to 'Show', submitter %></td>
<td><%= link_to 'Edit', edit_submitter_path(submitter) %></td>
<td><%= link_to 'Destroy', submitter, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
As a side note I have no problem showing the IDs of the emails suffix but obviously I'm trying to show the actual suffix instead:
<td><%= submitter.email %><%= submitter.email_suffix_id %></td>
Submitter.rb has a has many :emails and Email.rb has a belongs_to :submitter association.
So let's break this down. You say you're trying to do
submitter.email.suffix
however, your model has no such :email association. Instead you've stated your Submitter model has
has_many :emails
This would mean:
To print the :email attribute on some Submitter instance submitter, use
puts submitter.email
To print the :prefix attribute of each Email instance related to submitter via your has_many association, you'd need to loop over them
submitter.emails.each do |e|
puts e.suffix
done
It's still quite unclear what exactly you're trying to do, but hopefully this clears up some confusion on your end; there is no method naming conflict so far.
submitter.email.suffix won't work because it returns Submitter "email" which is a string, not the Email instance.
submitter.emails.each(&:suffix)
should work. submitter.emails will return an Email's instances and they do have suffix method(according to your words).

Rails - Accessing one to many variables in a table loop

I've got a simple one to many relationship between tasks and priorities.
class Task < ActiveRecord::Base
attr_accessible :subject, :body, :priority_id, :company_id, :status_id, :user_ids
has_and_belongs_to_many :users
belongs_to :priority
belongs_to :company
belongs_to :status
end
class Priority < ActiveRecord::Base
attr_accessible :name
has_many :tasks
end
From my tasks/show.html.erb view
<%= #task.priority.name %>
This works.
However in my tasks/index.html.erb
<% #tasks.each do |task| %>
<tr>
<td><%= task.subject %></td>
<td><%= task.body %></td>
<td><%= task.priority.name %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
The call to task.priority.name does not work.
It throws a NoMethodError.
I'm guessing this is a really stupid newbie mistake but I can't for the life of me figure it out. It would have assumed that the Active Record magic would have filtered down into a look like this.
I guess your NoMethodError comes on nil:NilClass. Try:
<%= task.priority.name if task.priority %>
Are you sure you have well created all the models and links with primary keys in your database? I think you link to a task with a priority id (prority_id) which doesn't exists.
Check your datas.
Given that there is no validation of the presence of priority in task, it's quite possible that you are calling "name" on nil. Validate the presence of priority (with validates_presence_of) or check it exists before printing it as Christoph says.

Resources