2016-03-30 12 views
1

この質問は私の最古のQuestion hereにリンクしています。jRuby/Ruby onデータベース関係とデータ検索

Iはモデル「USER」及び足場「ContactDetail」以下のように互いに関連を有しています。

  • ユーザーにhas_manyContactDetail
  • ContactDetailユーザー

私の質問belongs_toの

ログインに成功すると、すべての連絡先の詳細を自分のhtmlにロードするにはどうすればよいですか?ユーザーが新しいユーザーで、これまでの連絡先の詳細がない場合は、contact_detailsのnew.html.erbを自分のhtml内に表示します。それをしてもいいですか?

これは私がこれまでに試したものです。私のログイン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

あなたはすでに何をあなたを持っています必要、かなり。ここにいくつかの変更があります: フォームを表示しているコードは、あなたのモデルに関する詳細がないので、正確ではなく概念を説明することだけを覚えておいてください。

オプション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を与えます

se ssioncontroller.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 %> 
関連する問題