简体   繁体   English

将参数哈希设置为控制器内的变量(导轨)

[英]Setting a params hash to a variable within the controller (rails)

I'm very new to rails and have this fairly basic question: 我是Rails的新手,有一个相当基本的问题:

I have a form that takes in an array: 我有一个采用数组的形式:

<td><%= fields_for "days" do |form| %>
            M: <%= form.check_box "", {}, 'M', ''  %>
            T: <%= form.check_box "", {}, 'T', '' %>
            W: <%= form.check_box "", {}, 'W', '' %>
            Th: <%= form.check_box "", {}, 'Th', '' %>
            F: <%= form.check_box "", {}, 'F', '' %>
    <% end %>

This should be accessible through params[:days]. 这可以通过params [:days]访问。 How can I assign params[:days] to a variable within the controller? 如何在控制器中为变量分配params [:days]? @days is not correct here, right? @days在这里正确,对吗? (There's no Days object, so there's no instance of that object). (没有Days对象,因此没有该对象的实例)。 What should go in the [correct_variable] slot below? 下面的[correct_variable]位置应该显示什么?

[correct_variable] = params[:days] 

Thanks! 谢谢!


In response to comments: 回应评论:

I tried using @days, but for some reason it wouldn't get called where I'd like it to. 我尝试使用@days,但是由于某种原因,它不会在我想要的地方被调用。 In particular, I'd like to pass it to my Search model: 特别是,我想将其传递给我的搜索模型:

class Search < ActiveRecord::Base
  attr_accessible :name, :day, :units, :instructor

  def courses
    @courses ||= find_courses
  end 

  private 

    def find_courses
      Course.find(:all, :conditions => conditions)
    end   

    def day_conditions
      ["courses.day LIKE ?", "%"+ @days.join("%")+"%"] 
    end

I first instantiate @days in my courses controller (which is connected, through the index method, to a partial that uses an instance of the Search object. 首先,我在课程控制器中实例化@days(通过index方法连接到使用Search对象实例的部分控制器)。


More code: 更多代码:

From courses/index.html.erb: 从courses / index.html.erb:

<% if @search.save %>
    <div id = "results_scroll">
        <%= render :partial => 'searches/search_results' %>
    </div>
<% else %>

From search_results partial: 来自search_results部分:

<% "Search" %>
<table>
    <tr>
        <th align="left" width="25%"><%= 'Name' %></th>
        <th align="left" width="10%"><%= 'Number' %></th>
        <th align="left" width="20%"><%= 'Instructor' %></th>
        <th align="left" width="10%"><%= 'Room' %></th>
        <th align="left" width="5%"><%=  'Day' %></th>
        <th align="left" width="5%"><%=  'Units' %></th>
        <th align="left" width="15%"><%=  'Time' %></th>
        <th align="left" width="10%"><%=  'Limitations' %></th>
    </tr>
<%= render :partial => @search_show.courses %>
</table>

From the courses controller (this has the @search_show variable). 从课程控制器(具有@search_show变量)。

  def index
    @courses = Course.order(sort_column + " " + sort_direction)
    @search = Search.new 
    @search_show = Search.last
    @title = "List"
    @days = params[:days]

Finally, the _course.html.erb partial: 最后,_course.html.erb部分:

<tr>
   <td><%= course.name %></td>
   <td><%= course.number %></td>
   <td><%= course.instructor %></td>
   <td><%= course.room %></td>
   <td><%= course.day %></td>
   <td><%= course.units %></td>
   <td><%= course.time %></td>
   <td><%= course.limitations %></td>
   <td><%= link_to 'Show', course %></td>
</tr>

通过使用$ days全局变量而不是@days解决了该问题。

This is not really an answer, but there is some problems with how you are trying to access your @days variable you set in the controller. 这并不是一个真正的答案,但是您尝试访问在控制器中设置的@days变量的方式存在一些问题。

# your controller 
...
def index
  ...
  @days = params[:days]

# your model
...
private

def day_conditions
  ["courses.day LIKE ?", "%"+ @days.join("%")+"%"] 
end

The @days , in your model, you are calling is not going to access the @days in your controller. @days在模型中,您要调用的将不会访问控制器中的@days Each @days belongs to their respective instances. 每个@days属于各自的实例。

You should find a method of sending the @days from your controller into your models, instead of using global variables. 您应该找到一种将@days从控制器发送到模型中的方法,而不是使用全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM