我很新的軌道,並有着這樣一種很基本的問題:設置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