jRuby/Ruby on rails database relations and data re

2019-07-26 07:01发布

问题:

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)

回答1:

You already have what you need, pretty much. Here are some of the changes: Just remember the code that is showing form is to explain the concept and not exact, as I don't have details about your model.

Option 1 : Render the form inline

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.present? %>  <!-- Notice this? much cleaner -->
        <!-- Same code you have -->
    <% else %>  
        <p> show create option </p>

          <!-- You can just render the form here like so if you want -->

          <%= form_for(current_user.contact_details.build) do |f| %>
          <p>
            <%= f.label :name %><br>
            <%= f.text_field :name %>
          </p>
          <p>
            <%= f.label :phonenumber  %><br>
            <%= f.text_field :phonenumber  %>
          </p>
          <p>
            <%= f.submit %>
          </p>
        <% end %>

    <% end %>

<% else %>
    <!-- prompt users to login or signup here -->
<% end %>

Option 2 : Include the partial and give it @contact_detail variable

sessioncontroller.rb

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])

    #new change
    @contact_detail = current_user.contact_details.build if @contacts.empty?

    redirect_to root_url, notice: "Logged in!"
  else
    render :new
  end
 end

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.present? %>  <!-- Notice this? much cleaner -->
        <!-- Same code you have -->
    <% else %>  
        <p> show create option </p>

        <!-- Just render partial contact_details/new.html.erb here -->
        <%= render 'contact_details/new' %>
    <% end %>

<% else %>
    <!-- prompt users to login or signup here -->
<% end %>