[英]jRuby/Ruby on rails database relations and data retrieve
This question links to my earliest Question here .这个问题链接到我最早的问题here 。
I have a model "User" and scaffold "ContactDetail" which relate to each other as below.我有一个模型“用户”和脚手架“ContactDetail” ,它们相互关联如下。
My Question我的问题
How can I load all the contact details to my html upon successful login?如何在成功登录后将所有联系方式加载到我的 html 中? 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.如果用户是新用户并且到目前为止没有联系方式,我想在我的 html 中显示 contact_details' new.html.erb 。 Can I do that?我可以这样做吗?
This is what I tried so far In my login sessioncontroller.rb I create @contacts;这是我到目前为止所尝试的 在我的登录 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
And in my_contacts.html.erb在 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)登录、注销和会话正在我的尝试中工作)
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选项 1:内联呈现表单
my_contacts.html.erb 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选项 2:包括部分并给它@contact_detail 变量
sessioncontroller.rb会话控制器.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 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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.