0

hy 我尝试在我的应用程序上创建一个简单的搜索

日历.rb

def self.search(search_condition)
  search_condition = "%" + search + "%"
  find( :all, :conditions => ['city like ?', search_condition])
end

日历控制器.rb

def index
  @search = @user.calendar.search(params[:search_condition])
  @content_calendars = @user.calendar.all
  @content_calendars_by_dates = @content_calendars.group_by(&:published_on)
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end

部分_search.html.erb

<%= form_tag root_path, :method => 'get'  do %>
  <%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
  <%= submit_tag "Search", :name => nil %>   
<% end %

当我尝试时,我收到此错误:

The request method POST is inappropriate for the URL /. 我不明白为什么?

耙路线

user_calendars GET    /users/:user_id/calendars(.:format)   calendars#index

POST   /users/:user_id/calendars(.:format)          calendars#create

new_user_calendar GET    /users/:user_id/calendars/new(.:format)      calendars#new

edit_user_calendar GET    /users/:user_id/calendars/:id/edit(.:format) calendars#edit

user_calendar GET    /users/:user_id/calendars/:id(.:format)      calendars#show
4

1 回答 1

0

您的表单标签需要路由到日历索引操作。像这样:

<%= form_tag(user_calendars_path(@user), :method => 'get')  do %>
  <%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
  <%= submit_tag "Search", :name => nil %> 

<% end %>

编辑:想通了,这是真正的问题:

您的 calendars#create 路线与 calendars#index 路线相同。

/users/:user_id/calendars(.:format) 

您需要更改您的 routes.rb 文件以区分它们。传统上,复数路径转到索引操作。

于 2013-10-22T19:37:34.393 回答