request.post的问题

时间:2011-02-21 10:30:34

标签: ruby-on-rails

我有一个具有以下索引操作的控制器:

def index
  if request.post?
    flash[:notice] = "Searching.."
    redirect_to songs_path # where songs_path is this index page that i am on 
  end
end

在我的应用程序布局中,我已经定义了flash部分

<% if flash[:notice] %>
  <div id='notice'><%= flash[:notice] %></div>
<% end %>

在我的pages_path上我有

<% form_for :search do |f| %>
  <%= f.label :search_text %>
  <%= f.text_field :search_text %>
  <p>
    <%= f.submit "Search" %>
  </p>
<% end %>

最终的结果应该是通过youtube api(youtube-g)进行搜索,但现在我只希望在点击“搜索”按钮时显示该Flash通知,但它没有...任何想法为什么?

2 个答案:

答案 0 :(得分:0)

普通的索引动作是GET请求。因此,如果在您的routes.rb中有类似resources :searches的内容,那么您的代码将无效。

试试这个:

def create
  render :text => "Searching.."
end

因为POST /搜索将引用创建动作

答案 1 :(得分:0)

您应该将RESTful路由更新为

resources :songs do 
    collection do 
        get 'search'
    end
end

然后在您的控制器中

def search
    # Perform Your search here like Youtube ..
    flash[:notice] = "Searching.."
    redirect_to songs_path # This should work now.
end