Ruby on Rails Thursday, July 7, 2016

> On Jul 7, 2016, at 8:14 AM, Ruth Stephenson <lists@ruby-forum.com> wrote:
>
> I have a users (Devise) and apointments table. I want to add a link in
> my application.html.erb to link to that particular user's appointment,
> so they can have the option to change it. (As a customer).
>
> I have a similar link set up for them to edit their profile.
>
> <%= link_to 'My Appointments',
> appointment_path(current_user.appointment) if current_user.appointment
> %>
>
> Profile works:
> <%= link_to 'My Profile', profile_path(current_user.profile) if
> current_user.profile %>
>
>
> However the appointments link won't work. It gives the error:
> Couldn't find Appointment with
> 'id'=#<Appointment::ActiveRecord_Associations_CollectionProxy:0x007ffaa5999b28>
>
> Appointment belongs to user
> user has many appointments

This is why the link doesn't work. A user (current_user) does not have _an_ appointment, she has _many_ appointments. If you want the latest one, then use

current_user.appointments.order(:appointment_date).last if current_user.appointments.any?

You could put that in a helper so it doesn't make your views so ungainly.

Walter

> profile belongs to user
> user has one profile
>
> My code is as follows:
>
>
> def index
> @appointments = Appointment.all
> end
>
>
> def show
> @appointment = Appointment.find(params[:id])
>
> end
>
> def new
> @appointment = Appointment.new
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e5ecd6b53c92eedb2aeded777552ac49%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1445574D-EB3E-472F-99E0-998CBAD33264%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment