konung
konung

Reputation: 7046

Using simple-navigation gem in Rails 3 display proper menu on render :create

I'm using simple-navigation gem in my Rails 3 app. It works fine except for one little quirk.

I have a sidebar div that contains a second level of menu, that get's displayed !render_navigation(:level => 2).nil?

It works fine except in one special case. Let's say I have a model ContactAttempt. A regular user (even if they are not signed in) is able to create a new ContactAttempt , in controller I save the record and then a notification email is sent to an appropriate department. Also and admin is able to list all records (index) and look at each one ( show), edit and update are disabled. This is just a contact form there is no need to edit or update records for other than malicious reasons.

Here is how my navigation is setup right now ( from navigation.rb)

primary.item :contact_us, 'Contact Us', new_contact_attempt_path do |secondary|
  secondary.item :send_email, 'Send Email', new_contact_attempt_path
  if user_signed_in? and current_user.admin?
    secondary.item :list_contact_attempts, 'List of Contact Attempts', contact_attempts_path 
  end  
end  

My problem is that when user tries to submit info that doesn't pass validation (like without return email, or message containing illegal characters) the form is rendered again with all the error indicated. However even thou the current_action == "create" , the actual url becomes "localhost:3000/contact_attempts . Which means that to simple- navigation it looks like the path is contact_attempt_path , and secondary.item :new_contact_attempt is not generated. I tried to solve it using highlights_on => /contact_attempts which forces item to get generated on all views but then it highlights all the time on all actions - which is confusing.

Does anyone have any idea how to solve it?

EDIT

Ok I was able to figure out a way to achieve what I want - but it seems cumbersome, and I don't really like it:

primary.item :contact_us, 'Contact Us', new_contact_attempt_path do |secondary|
  if user_signed_in? and current_user.admin?
    if params[:action] == ("create" or "new" )
      secondary.item :send_email, 'Send Email', new_contact_attempt_path, :highlights_on => /contact_attempts/ 
      secondary.item :list_contact_attempts, 'List of Contact Attempts', contact_attempts_path, :highlights_on => /contact_attempts\/index/
    else
      secondary.item :send_email, 'Send Email', new_contact_attempt_path
      secondary.item :list_contact_attempts, 'List of Contact Attempts', contact_attempts_path
    end  
  else
    secondary.item :send_email, 'Send Email', new_contact_attempt_path, :highlights_on => /contact_attempts/   
  end  
end 

I'll mark this as answered tomorrow, if nobody comes back with a proper solution. :-(

Upvotes: 1

Views: 1638

Answers (1)

konung
konung

Reputation: 7046

Ok I was able to figure out a way to achieve what I want - but it seems cumbersome, and I don't really like it:

primary.item :contact_us, 'Contact Us', new_contact_attempt_path do |secondary|
  if user_signed_in? and current_user.admin?
    if params[:action] == ("create" or "new" )
      secondary.item :send_email, 'Send Email', new_contact_attempt_path, :highlights_on => /contact_attempts/ 
      secondary.item :list_contact_attempts, 'List of Contact Attempts', contact_attempts_path, :highlights_on => /contact_attempts\/index/
    else
      secondary.item :send_email, 'Send Email', new_contact_attempt_path
      secondary.item :list_contact_attempts, 'List of Contact Attempts', contact_attempts_path
    end  
  else
    secondary.item :send_email, 'Send Email', new_contact_attempt_path, :highlights_on => /contact_attempts/   
  end  
end 

I'll mark this as answered tomorrow, if nobody comes back with a proper solution. :-(

Seems to be the best solution right now, as it is not implemented in the gem. Opened a ticket on github for andi(creator).

Upvotes: 4

Related Questions