Rails:在视图中的collection_select中选择保存值

时间:2014-06-03 02:04:42

标签: ruby-on-rails-4 views collection-select

我是新手,我今天大部分时间都在努力寻找问题的答案,但我无法弄明白。我认为这是一个常见的问题,很多人可能正在寻找直截了当的答案,所以这里有:

我想要做的就是使用collection_select语句,将选定的值存储在@bno之类的变量中,并使用相同的变量过滤掉属于建筑物的属性。

这是我的代码:

<%= collection_select :property, :buildingnumber, Building.all, :streetno, :streetno, {prompt: "Building Number"},{:class => "form-control"}%> 

据我所知,通过查看SO, params [:property] [:buildingnumber] 现在应该保留我的选择。

现在再往下,我有这段代码:

<% @properties.filter_by_building(buildingnumber: Property.find(params[:property][:buildingnumber])).each do |property| %>
             <div class="col-sm-6 col-md-4">
              <div class="thumbnail relative">
                <%= image_tag property.avatar.url(:medium) %>
                <div class="caption fixedtextheight">
                  <h4>Building No: <%= property.buildingnumber%></h4><h4>Property No: <%= property.number%></h4>

                  <p><h5>Rented: <%=property.rented%></h5></p>
                  <p>
                    <a type="button" class="btn btn-default navbar-btn" href=<%= edit_property_path(property) %>>Edit</a> <%= link_to "Delete", property, method: :delete, :class=>"btn btn-default navbar-btn", data: { confirm: "Deleting this property will delete ALL the tenants, expenses, and leases associated to it."} %>   
                  </p>   
                </div>
              </div>
             </div>

          <%end%>

这是我得到的错误:

Properties#index中的NoMethodError 显示/用户//桌面/ JR建筑/ jrbuilding / app / views / properties / index.html.erb第31行引出:

未定义的方法`[]&#39;为零:NilClass 提取的来源(第31行):

                 <% @properties.filter_by_building(buildingnumber: Property.find(params[:property][:buildingnumber])).each do |property| %>

我对如何在视图中保存局部变量感到困惑,因为我来自Java / Obj C背景,我想做一些非常简单的事情,即基于页面过滤关于用户在collection_select中选择的值。另外,我需要弄清楚如何以某种方式更新页面。

无论如何有用的提示非常感谢,提前谢谢!

还想在我的属性模型中向您显示我的filter_by_building代码:

def self.filter_by_building(opts={})
    buildingNo = opts[:buildingnumber]
    building = Building.arel_table

    self.where(:buildingnumber => buildingNo)
end

非常感谢!

0 个答案:

没有答案