This question links to my earliest Question here.
I have a model "User" and scaffold "ContactDetail" which relate to each other as below.
- Each User has_many ContactDetail
- ContactDetail belongs_to User
My Question
How can I load all the contact details to my html upon successful login? If user is a new user and has no contact details so far, I want to show contact_details' new.html.erb inside my html. Can I do that?
This is what I tried so far In my login sessioncontroller.rb I create @contacts;
def create
user = User.find_by_email(params[:email])
if user && user.email === params[:email] && user.password === params[:password]
session[:user_id] = user.id
@contacts = ContactDetail.find_by_id(session[:user_id])
redirect_to root_url, notice: "Logged in!"
else
render :new
end
end
And in my_contacts.html.erb
<% if current_user %>
<p> You're logged in as : <%= current_user.email %> <%= link_to "Log Out", logout_path %> </p>
<p> Here you can maintain your own contact details. gfdgdfgfd fdgdg sfgs q34 adg fg fs g wer rewr ererq q </p>
<% if @contacts != nil %>
<% @contacts.each do |contact| %>
<%session[:contact_id]=contact.id%>
<table>
<tbody>
<tr>
<td><%=contact.id%></td>
<td><%=contact.name%></td>
<td><%= contact.phonenumber %></td>
<td><%=link_to "edit", edit_path(:param1=>contact.id) %></td>
<td><%=link_to "delete",delete_path(:param1=>contact.id),:confirm => "Are you sure ?" %></td>
</tr>
</tbody>
</table>
<% end %>
<% else %>
<p> show create option </p>
<!-- i want to render contact_details/new.html.erb here -->
<% end %>
<% else %>
<!-- prompt users to login or signup here -->
<% end %>
Note
Login, logout and sessions are working in my attempt)