jRuby / Ruby on rails数据库关系和数据检索

时间:2016-03-30 03:55:14

标签: ruby-on-rails jruby jrubyonrails

这个问题链接到我最早的Question here

我有一个模型“用户”和脚手架“ContactDetail”,它们彼此相关,如下所示。

  • 每位用户 has_many ContactDetail
  • ContactDetail belongs_to 用户

我的问题

成功登录后如何将所有联系人详细信息加载到我的html中?如果用户是新用户并且到目前为止没有联系人详细信息,我想在我的html中显示contact_details'new.html.erb。我能这样做吗?

这是我到目前为止所尝试的在我的登录sessioncontroller.rb中我创建@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

在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 %>

注意

登录,注销和会话正在我的尝试中工作)

1 个答案:

答案 0 :(得分:1)

你已经拥有了所需要的东西。以下是一些变化: 请记住,显示表单的代码是解释概念而不是确切的,因为我没有关于您的模型的详细信息。

选项1:以内嵌方式呈现表单

  

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 %>

选项2:包含部分并给它@contact_detail变量

  

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 %>