Rails show raised exception in form view












0















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?



enter image description here










share|improve this question























  • 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
















0















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?



enter image description here










share|improve this question























  • 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














0












0








0








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?



enter image description here










share|improve this question














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?



enter image description here







ruby-on-rails ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 19:08









Trenton TylerTrenton Tyler

91811035




91811035













  • 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









3














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.






share|improve this answer


























  • 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
















3














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.






share|improve this answer


























  • 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














3












3








3







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno