我很新的轨道,并有着这样一种很基本的问题:设置params散列,给一个变量控制器内(轨)
我有一个表格,它接受一个数组:
<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 %>
这应该通过params [:days]来访问。如何将params [:days]赋给控制器中的变量? @days是不是在这里是正确的,对吧? (没有Days对象,所以没有该对象的实例)。什么应该在下面的[correct_variable]插槽中进行?
[correct_variable] = params[:days]
谢谢!
在回应评论:
我试着用@days,但由于某种原因,它不会被调用,我想它。具体地讲,我想将它传递给我的搜索模型:
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
我首先实例化在我的课程控制器@days(其连接,通过索引方法,涉及使用的一个实例的部分。搜索对象
更多代码:
从课程/ index.html.erb:
<% if @search.save %>
<div id = "results_scroll">
<%= render :partial => 'searches/search_results' %>
</div>
<% else %>
来自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>
从课程控制器(这有@search_show变量)。
def index
@courses = Course.order(sort_column + " " + sort_direction)
@search = Search.new
@search_show = Search.last
@title = "List"
@days = params[:days]
最后,_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>
我不是100%的你问什么。 '@days = params [:days]''有什么问题?你说没有“天对象”,但不是那个期望值?如果没有@天,那么你不会覆盖东西 – tzenes
添加一些评论我的问题。希望有所帮助。 – jyli7
这是在课程控制器?你在哪里进行部分呼叫?在courses/index.html.erb里面?你在哪里路由? “课程#指数”? – tzenes