{"action":"closed","issue":{"url":"https://api.github.com/repos/RachelScodes/mymix/issues/10","labels_url":"https://api.github.com/repos/RachelScodes/mymix/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/RachelScodes/mymix/issues/10/comments","events_url":"https://api.github.com/repos/RachelScodes/mymix/issues/10/events","html_url":"https://github.com/RachelScodes/mymix/issues/10","id":115745520,"number":10,"title":"How to create and render a custom error message","user":{"login":"RachelScodes","id":13145686,"avatar_url":"https://avatars.githubusercontent.com/u/13145686?v=3","gravatar_id":"","url":"https://api.github.com/users/RachelScodes","html_url":"https://github.com/RachelScodes","followers_url":"https://api.github.com/users/RachelScodes/followers","following_url":"https://api.github.com/users/RachelScodes/following{/other_user}","gists_url":"https://api.github.com/users/RachelScodes/gists{/gist_id}","starred_url":"https://api.github.com/users/RachelScodes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RachelScodes/subscriptions","organizations_url":"https://api.github.com/users/RachelScodes/orgs","repos_url":"https://api.github.com/users/RachelScodes/repos","events_url":"https://api.github.com/users/RachelScodes/events{/privacy}","received_events_url":"https://api.github.com/users/RachelScodes/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-11-08T17:26:05Z","updated_at":"2015-11-08T18:01:14Z","closed_at":"2015-11-08T18:01:14Z","body":"Ok, this is a brief question, but I'm pasting all the code so you don't have to go digging:\r\n\r\n#### Problem: Duplicate songs on Mixtapes, and deleting all duplicates at once\r\nI am trying to render a custom error message. Due to the nature of habtm relationships it's easy to have duplicates. so it was possible to have 12 copies of the same song on the same mixtape, and if you wanted to delete one, it would delete every instance of that song on that mixtape.\r\n\r\n#### Solving that problem, creates a new Problem...\r\nNow, I have a filter in the record method on my controller, so it won't add the same song twice, and therefore the mass deletion isn't a problem. Cool beans.\r\n\r\n#### User has no idea and thinks the page just reloads, but it's doing so much you guys!\r\nIf a user adds a song that's already on the mixtape, it goes through the motions, sees it as a duplicate, doesn't add it, renders the page... a lot of stuff happens, but to the user it just looks like the page reloads because there's no error message to let them know anything happened.\r\n\r\n### ISSUE:\r\nHow can I write my own error message, and pass that up the chain so it makes it to the page?\r\n\r\n#### Here's what I've got so far:\r\n\r\n##### Mixtape Model:\r\n```ruby\r\nclass Mixtape < ActiveRecord::Base\r\n   belongs_to :user\r\n\r\n   has_and_belongs_to_many :songs\r\n\r\n   validates :name, presence: true, uniqueness: {\r\n      case_sensitive: false,\r\n      message: \"is taken buddy. Mix it up!\" }\r\n\r\n   def record(song)\r\n      self.songs = (self.songs + [song]).uniq\r\n   end\r\n\r\n   def erase(song)\r\n      self.songs.destroy(song)\r\n   end\r\nend\r\n```\r\n\r\n##### Mixtape Controller:\r\n```ruby\r\n\r\n   def record_song\r\n\t\tsong = Song.find(params[:mixtapes_songs][:song_id])\r\n\r\n      # access mixtape correctly depending on referrer:\r\n      if ( URI(request.referer).path.match '/songs/' )\r\n         mixtape = Mixtape.find(params[:mixtapes_songs][:mixtape_id])\r\n      else\r\n\t\t   mixtape = Mixtape.find(params[:id])\r\n      end\r\n\r\n\t\tmixtape.record(song)\r\n\t\tredirect_to mixtape_path(mixtape)\r\n\tend\r\n\r\n\tdef erase_song\r\n\t\tmixtape = Mixtape.find(params[:id])\r\n\t\tsong = Song.find(params[:song_id])\r\n\t\tmixtape.erase(song)\r\n\t\tredirect_to mixtape_path(mixtape)\r\n\tend\r\n\r\n   private\r\n\r\n   def mixtape_params\r\n      params.require(:mixtape).permit(\r\n      :name,\r\n      :dedication,\r\n      :about,\r\n      :img_src,\r\n      :user_id,\r\n      :mixtape_songs,\r\n      :song_id,\r\n      :song\r\n      )\r\n   end\r\n```\r\n\r\n##### Mixtape Views:\r\n###### show:\r\n```html\r\nblah blah stuff\r\n<% if current_user %>\r\n   <%# display errors from adding songs %>\r\n   <% if @song %>\r\n      <%= render partial: 'layouts/errors_flash', :locals => { :noun => flash[:errors] } %>\r\n   <% end %>\r\n\r\n   <%# logged in user can add and create songs %>\r\n   <div class=\"song-form\" id=\"mixtape\">\r\n      <h3>Add a Song to <span class=\"mixtape\" id=\"name\"><%= @mixtape.name %>!</span></h3>\r\n      <%# select from library via drop-down %>\r\n      <%= render partial: 'mixtapes/song_select' %>\r\n      <%# or add new song %>\r\n      <%= render partial: 'mixtapes/song_add' %>\r\n   </div>\r\n\r\n<% else %>\r\nboo hoo you're not logged in gfy basically\r\n```\r\n\r\n###### drop down menu partial (aka song_select:\r\n```html\r\n<% if Song.all.length > 1 %>\r\n   <%= form_for :mixtapes_songs, url: record_song_mixtape_path do |f| %>\r\n      <%= f.label \"Choose a song from the Mix.r Library\" %><br/>\r\n      <%= f.collection_select(:song_id, Song.all, :id, :title) %>\r\n      <%= f.hidden_field :mixtape_id, :value => @mixtape.id%>\r\n      <%= f.submit \"Add song to #{@mixtape.name}\"%>\r\n   <% end %>\r\n<% end %>\r\n```\r\n\r\n#### I already have 2 error message partials, one for objects with errors from a create action, and one for flash errors\r\n###### create (noun )error message partial:\r\n```html\r\n<% if noun && noun.errors.messages.length > 0 %>\r\n   <div class=\"errors\">\r\n      <% noun.errors.full_messages.each do |error_msg| %>\r\n         <p class=\"error\"><%= error_msg %><p>\r\n      <% end %>\r\n   </div>\r\n<% end %>\r\n```\r\n\r\n###### flash error message partial:\r\n```html\r\n<% if noun && noun.length > 0 %>\r\n   <div class=\"errors\">\r\n      <% noun.each do |obj,errors| %>\r\n         <p class=\"error\"><%= obj.capitalize %>:\r\n            <% errors.each do |m| %>\r\n            <li><%= m %></li>\r\n            <% end %>\r\n         </p>\r\n      <% end %>\r\n   </div>\r\n<% end %>\r\n```\r\n\r\n#### QUESTION:\r\n~~Tell me what you think about me, I buy my own diamonds and I buy my own rings.~~\r\n\r\nhow and where would I store... what? what would I even store? and object? a string? to be able to render an error message to the user knows they tried unsuccessfully to add a duplicate song via the drop down?"}}