Rails show raised exception in form view
In my application a user can belong to a team. A captain can invite any one user to any one team as long as that user doesn't already belong to a team. I have an invite model that will check if that user belongs to a team before sending the invite.
class Invite < ApplicationRecord
belongs_to :team
belongs_to :user
before_create :check_membership
def check_membership
@memberships = self.user.teams.map { |t| t.id }
if @memberships.include?(self.team.id)
raise 'That user is already on a team'
end
end
end
If the user does belong to a team and is invited by a captain, I want there to be an error message that alerts the captain they are already on a team.
To do this I created a method to check if the user belongs to a team and, if they do, to raise an exception. I would like to show this exception in the view by putting this in the form.
<% if invite.errors.any? %>
<ul>
<% test.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
When I send the invite to a test user, it just shows that error message in the typical rails error page rather than showing the error message above the form in re-render. How do I make this exception message show up in the form after submitting it?

ruby-on-rails ruby
add a comment |
In my application a user can belong to a team. A captain can invite any one user to any one team as long as that user doesn't already belong to a team. I have an invite model that will check if that user belongs to a team before sending the invite.
class Invite < ApplicationRecord
belongs_to :team
belongs_to :user
before_create :check_membership
def check_membership
@memberships = self.user.teams.map { |t| t.id }
if @memberships.include?(self.team.id)
raise 'That user is already on a team'
end
end
end
If the user does belong to a team and is invited by a captain, I want there to be an error message that alerts the captain they are already on a team.
To do this I created a method to check if the user belongs to a team and, if they do, to raise an exception. I would like to show this exception in the view by putting this in the form.
<% if invite.errors.any? %>
<ul>
<% test.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
When I send the invite to a test user, it just shows that error message in the typical rails error page rather than showing the error message above the form in re-render. How do I make this exception message show up in the form after submitting it?

ruby-on-rails ruby
Theerrorsreferenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.
– ABMagil
Nov 24 '18 at 19:10
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33
add a comment |
In my application a user can belong to a team. A captain can invite any one user to any one team as long as that user doesn't already belong to a team. I have an invite model that will check if that user belongs to a team before sending the invite.
class Invite < ApplicationRecord
belongs_to :team
belongs_to :user
before_create :check_membership
def check_membership
@memberships = self.user.teams.map { |t| t.id }
if @memberships.include?(self.team.id)
raise 'That user is already on a team'
end
end
end
If the user does belong to a team and is invited by a captain, I want there to be an error message that alerts the captain they are already on a team.
To do this I created a method to check if the user belongs to a team and, if they do, to raise an exception. I would like to show this exception in the view by putting this in the form.
<% if invite.errors.any? %>
<ul>
<% test.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
When I send the invite to a test user, it just shows that error message in the typical rails error page rather than showing the error message above the form in re-render. How do I make this exception message show up in the form after submitting it?

ruby-on-rails ruby
In my application a user can belong to a team. A captain can invite any one user to any one team as long as that user doesn't already belong to a team. I have an invite model that will check if that user belongs to a team before sending the invite.
class Invite < ApplicationRecord
belongs_to :team
belongs_to :user
before_create :check_membership
def check_membership
@memberships = self.user.teams.map { |t| t.id }
if @memberships.include?(self.team.id)
raise 'That user is already on a team'
end
end
end
If the user does belong to a team and is invited by a captain, I want there to be an error message that alerts the captain they are already on a team.
To do this I created a method to check if the user belongs to a team and, if they do, to raise an exception. I would like to show this exception in the view by putting this in the form.
<% if invite.errors.any? %>
<ul>
<% test.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
When I send the invite to a test user, it just shows that error message in the typical rails error page rather than showing the error message above the form in re-render. How do I make this exception message show up in the form after submitting it?

ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 24 '18 at 19:08
Trenton TylerTrenton Tyler
91811035
91811035
Theerrorsreferenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.
– ABMagil
Nov 24 '18 at 19:10
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33
add a comment |
Theerrorsreferenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.
– ABMagil
Nov 24 '18 at 19:10
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33
The
errors referenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.– ABMagil
Nov 24 '18 at 19:10
The
errors referenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.– ABMagil
Nov 24 '18 at 19:10
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33
add a comment |
1 Answer
1
active
oldest
votes
You don't want to raise an exception, you just want to add a validation to the Invite model.
validate :check_membership
def check_membership
return if persisted?
if user.teams.include?(team)
errors.add(:base, 'The user is already on the team')
end
end
The errors for invite will then include the above error if it occurs.
You can indeed trap conditions by raising an exception, but that will, by design, halt the program unless you rescue from the exception. Which you could do, but that's overkill for tihs case.
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461510%2frails-show-raised-exception-in-form-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't want to raise an exception, you just want to add a validation to the Invite model.
validate :check_membership
def check_membership
return if persisted?
if user.teams.include?(team)
errors.add(:base, 'The user is already on the team')
end
end
The errors for invite will then include the above error if it occurs.
You can indeed trap conditions by raising an exception, but that will, by design, halt the program unless you rescue from the exception. Which you could do, but that's overkill for tihs case.
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
add a comment |
You don't want to raise an exception, you just want to add a validation to the Invite model.
validate :check_membership
def check_membership
return if persisted?
if user.teams.include?(team)
errors.add(:base, 'The user is already on the team')
end
end
The errors for invite will then include the above error if it occurs.
You can indeed trap conditions by raising an exception, but that will, by design, halt the program unless you rescue from the exception. Which you could do, but that's overkill for tihs case.
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
add a comment |
You don't want to raise an exception, you just want to add a validation to the Invite model.
validate :check_membership
def check_membership
return if persisted?
if user.teams.include?(team)
errors.add(:base, 'The user is already on the team')
end
end
The errors for invite will then include the above error if it occurs.
You can indeed trap conditions by raising an exception, but that will, by design, halt the program unless you rescue from the exception. Which you could do, but that's overkill for tihs case.
You don't want to raise an exception, you just want to add a validation to the Invite model.
validate :check_membership
def check_membership
return if persisted?
if user.teams.include?(team)
errors.add(:base, 'The user is already on the team')
end
end
The errors for invite will then include the above error if it occurs.
You can indeed trap conditions by raising an exception, but that will, by design, halt the program unless you rescue from the exception. Which you could do, but that's overkill for tihs case.
edited Nov 26 '18 at 8:54
answered Nov 24 '18 at 19:22
SteveTurczynSteveTurczyn
26.9k42738
26.9k42738
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
add a comment |
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
By the way, I note the requirement for this only to happen on create, and have edited the answer to reflect that.
– SteveTurczyn
Nov 26 '18 at 8:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461510%2frails-show-raised-exception-in-form-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The
errorsreferenced in the code is guides.rubyonrails.org/…, not RuntimeExceptions. It gets populated by validating your model.– ABMagil
Nov 24 '18 at 19:10
An exception is the wrong approach here; this is validation, as Steve says. Exceptions are for exceptional cases, not things that could trivially happen based on user input.
– Dave Newton
Nov 24 '18 at 19:33