我将搜索结果填充到我的页面下方 - 网站访问者在我的搜索栏中输入关键字以搜索用户,并在此页面上生成结果。在找到匹配结果后,用户的数据将填充到下面的div中。也就是说,当访问者点击" Go" button(go.png),我希望检索到的数据在同一布局内的下一页(book.html.erb)上发布。如何将数据从_user.html.erb传递到book.html.erb?
_user.html.erb
<div class="card">
<div class="columns">
<div class="col">
<%= image_tag user.avatar.url(:thumb), :class => 'round-image-125' %>
<br><br>
<font color="#ee4d50" font size="3"><b>Services:</b>
</div>
<div class="col">
<div class="name">
<%= link_to user.firstname, user %> <%= user.lastname %> <a href="users/book"><img src="/assets/go.png" style="float:right; padding-right:15px" border="0"></a>
</div>
<br>
<font color="#4d4d4d" font size="3"><b><%= user.city%>, <%= user.stateprov%></font></b>
<br><br>
<font color="#2c78d3" font size="4"><b>Bio:</b></font>
<br><br>
<%= user.bio%>
<br><br>
<font color="#2c78d3" font size="4"><b>Categories:</b></font>
</div>
</div>
</div>
用户/ index.html.erb
<div class="browse-subheader">
<% if @user.present? %>
<%= render @user %>
<% else %>
<p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>
book.html.erb
<div class="card">
<div class="columns">
<div class="col">
<%= image_tag user.avatar.url(:thumb), :class => 'round-image-125' %>
<br><br>
<font color="#ee4d50" font size="3"><b>Services:</b>
</div>
<div class="col">
<div class="name">
<%= link_to user.firstname, user %> <%= user.lastname %> <a href="users/book"><img src="/assets/go.png" style="float:right; padding-right:15px" border="0"></a>
</div>
<br>
<font color="#4d4d4d" font size="3"><b><%= user.city%>, <%= user.stateprov%></font></b>
<br><br>
<font color="#2c78d3" font size="4"><b>Bio:</b></font>
<br><br>
<%= user.bio%>
<br><br>
<font color="#2c78d3" font size="4"><b>Categories:</b></font>
</div>
</div>
</div>
users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "You signed up successfully"
flash[:color] = "valid"
redirect_to @user
else
flash[:notice] = "Form is invalid"
flash[:color] = "invalid"
render "new"
end
end
def index
@user = User.all
if params[:search]
@user = User.search(params[:search]).order("created_at DESC")
else
@user = User.all.order('created_at DESC')
end
end
def book
@user = User.new
@category = Category.new
end
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
else
render 'edit'
end
end
答案 0 :(得分:1)
您的控制器“书籍”操作不会查找_user视图中找到的用户。它正在创建一个新的。
尝试更换:
@user = User.new
by:
@user = User.find(params[:id])
还可以通过在书籍视图中的用户变量之前添加@符号来引用实例变量。例如@ user.lastname而不是user.lastname(如果我没有记错,用户范围只是视图,因为@user范围跨越视图和控制器)