我需要知道你如何制作一个表格,你可以选择一个记录,根据该记录做一些计算并显示结果(最好是在同一页面上)。
为了使其尽可能简单,我想说我想从用户模型中选择一条记录并显示他的大写电子邮件。
所以结构将是:
capitalized_controller.rb
class CapitalizedController < ApplicationController
before_action :authenticate_user!
def users
@users = User.all
end
end
大写/ users.html.erb:
<%= form_tag('/capitalized/users') do %>
<div class="controls">
<%= select_tag :email, options_from_collection_for_select(@users, "id", "email"), :class => 'text_field' %>
</div>
<div class="form-actions">
<%= submit_tag nil, :class => 'btn btn-primary' %>
</div>
<% end %>
它显示如下:
所以,直到这里它显示了按字母顺序排序的用户电子邮件列表,我可以点击发送按钮(“Enviar”)并且它工作正常(我到目前为止没有错误),但它只会再次加载页面,我该怎么做才能显示大写的电子邮件?
答案 0 :(得分:0)
在你的控制器中更改:
def users
@users = User.all
@var_to_show_user = User.search(params[:search_user])
end
在您的模型中执行此操作:
def self.search(search_mail)
if search
where('mail = ?', search_mail )
else
scoped
end
end
在您看来:
<%= form_tag('/capitalized/users') do %>
<div class="controls"><%= select_tag :email, options_from_collection_for_select(@users, "id", "email"), :class => 'text_field' %></div>
<div class="form-actions"><%= submit_tag nil, :class => 'btn btn-primary' %></div>
<% end %>
<% @var_to_show_user.each do |result| %>
<%= result.mail.to_s.first.upcase %>
<% end %>
注意:
"mail" is the column's name
"to_s" is to convert to string
"first" is to show the first letter
"upcase" is to show the value capitalize
希望这有效......祝你好运。
答案 1 :(得分:0)
嗯,最后我唯一需要做的就是:
<强> capitalized_controller.rb 强>
class CapitalizedController < ApplicationController
before_action :authenticate_user!
def users
@users = User.all
if params[:user]
@result = User.all.find(params[:user])
@result = @result.email.capitalize!
else
@result = nil
end
end
end
<强>大写/ users.html.erb:强>
<%= form_tag('/capitalized/users', :method => "post", id: "capitalize-form") do %>
<div class="controls">
<%= select_tag :user, options_from_collection_for_select(User.all, "id", "email", params[:user]), :class => 'text_field' %>
</div>
<div class="form-actions">
<%= submit_tag 'Capitalize email', :class => 'btn btn-primary' %>
</div>
<% end %>
<% if @result %>
<p><%= @result %></p>
<% end %>
它显示如下: