jRuby/Ruby 关于 rails 数据库关系和数据检索
jRuby/Ruby on rails database relations and data retrieve
这个问题链接到我最早的Question here。
我有一个模型 "User" 和脚手架 "ContactDetail" 它们相互关联如下。
- 每个用户 has_many 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:内联呈现表单
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 %>
这个问题链接到我最早的Question here。
我有一个模型 "User" 和脚手架 "ContactDetail" 它们相互关联如下。
- 每个用户 has_many 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:内联呈现表单
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 %>