[英]View Change “On Submit” Rails
Is there a way to do this without javascript? 没有JavaScript,有没有办法做到这一点?
Let me explain. 让我解释。 I have two models, users and dogs.
我有两种模式,用户和狗。
I'm using the index as a home page for when the users are logged in. 我将索引用作用户登录时的主页。
There is a form that searches dogs (not by any information the user enters, it's prepopulated with dog data), but the index is listing the result of my search query on page load. 有一种搜索狗的形式(不是通过用户输入的任何信息,它是用狗数据预先填充的),但是索引列出的是我在页面加载时进行搜索的结果。 I'd like it to only show when the button is submitted.
我希望它仅在提交按钮时显示。
I'll show some code. 我将显示一些代码。
controller for dog: 狗的控制器:
def index
if current_user
@user = current_user
@breed = @user.dogs.first.primarybreed
# params[:search] = @breed
@dog = Dog.search(params[:search]).sample
end
end
view: 视图:
%= form_tag dogs_path, :method => :get do %>
<%= hidden_field_tag :search, @breed %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
<div id="dogswrap">
<h1><%= current_user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<td><%= image_tag @dog.image.url(:medium) %></td>
<td><%= @dog.name %></td>
<td><%= @dog.nick %></td>
<td><%= @dog.primarybreed %></td>
<td><%= @dog.secondarybreed %></td>
<td><%= @dog.age %></td>
<td><%= @dog.weight %></td>
<td><%= link_to "#{@dog.user.name}" %> </td>
<td><%= image_tag @dog.user.image.url(:thumb) %> </td>
</td>
</tr>
</tbody>
</table>
Model 模型
def self.search(search)
find(:all, :conditions => ['primarybreed LIKE ?', "%#{search}%"])
end
I would like to have an if statement that shows the table only after the submit has been pushed. 我希望有一个if语句,仅在推送提交后才显示表。
If there is another way to do this, I'd appreciate it. 如果还有其他方法可以做到,我将不胜感激。
Thanks! 谢谢!
EDIT: Still working on this. 编辑:仍在为此工作。 May use a render..?
可以使用渲染..?
You can just use where
in place of the way you're currently doing it find
and conditions
您可以使用
where
代替当前的find
和conditions
def self.search(search)
where('primarybreed LIKE ?', "%#{search}%")
end
One thing I've learnt when using the LIKE
function in development for SQLite3 and LIKE
in PostgreSQL for production, was LIKE
is case-insensitive in SQLite3 but not for PSQL. 有一件事我一直在使用的时候了解到
LIKE
发展为sqlite3的和功能LIKE
在PostgreSQL的生产,是LIKE
不区分大小写的sqlite3的,但不是PSQL。 You would need to use ILIKE
for that. 您将需要使用
ILIKE
。
To your main question. 对您的主要问题。 This is how I would do it.
这就是我要做的。
def index
if current_user
@user = current_user
@dogs = Dog.search(params[:search]) if params[:search].present?
end
end
For you views, start moving everything into a partial. 对于您的视图,开始将所有内容都移到局部。
_form.html.erb
and _dog.html.erb
_form.html.erb
和_dog.html.erb
form 形成
<%= form_tag dogs_path, :method => :get do %>
<%= text_field_tag :search %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
dog 狗
<td><%= image_tag dog.image.url(:medium) %></td>
<td><%= dog.name %></td>
<td><%= dog.nick %></td>
<td><%= dog.primarybreed %></td>
<td><%= dog.secondarybreed %></td>
<td><%= dog.age %></td>
<td><%= dog.weight %></td>
<td><%= link_to "#{dog.user.name}" %> </td>
<td><%= image_tag dog.user.image.url(:thumb) %></td>
Notice removed @dog
and replaced with dog
通知删除
@dog
并替换为dog
Index view 索引检视
<%= render 'form' %>
<div id="dogswrap">
<h1><%= @user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<% if @dogs %>
<%= render @dogs %>
<% end %>
</tr>
</tbody>
</table>
changed current_user.dogs[0]..
to @user.dogs[0]..
Also made a conditional for if the @dogs
instance variable is truthy. 将
current_user.dogs[0]..
更改为@user.dogs[0]..
也为@dogs
实例变量是否为真设置了条件。 If it is, then render will render out the _dog.html.erb
partial for each dog in the @dogs
collection 如果是,那么render将为
@dogs
集合中的每只狗渲染_dog.html.erb
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.