我对rails有一个小问题。我有一个搜索控制器,它搜索数据库中的名称,如果找到它显示有关它的详细信息,或者我重定向到新的名称页面。重定向后,搜索到的名称是否会自动显示在新表单页面中?
提前致谢。
答案 0 :(得分:2)
您可以使用ActionController::Flash在操作之间传递数据。
def search(searchedName)
# perform search on DB
flash[:searchedName] = searchedName
redirect_to new_name
end
def new_name
end
new_name.html.erb:
<% if flash[:searchedName] %>
<%= text_field_tag flash[:searchedName] %>
<% end %>
答案 1 :(得分:2)
好吧,假设您正在保存用户在名为@name的变量上输入的“名称”
因此,在执行搜索操作的控制器上,您执行了类似的操作:
if @name
...#action if the name was found
else
session[:name] = @name
...#here is the action you did to redirect
关于所谓方法的声明(你说它是'新'):
def new
@name = session[:name] if session[:name] #I don't know exactly if the condition is this one,
#but you have to confirm that the session was instatiated
session.delete(:name) #to don't let garbage
...#continuous the action of show the new page
#and on the page you have access to a variable @name, that have the name searched before.