如何根据rails中的条件重定向到同一页面

时间:2013-11-25 02:54:47

标签: ruby-on-rails haml

I have the following haml code in index.haml



  %nav.breadcrumbs
      %h2 Breadcrumbs
  %br
  -if @invalid
    %p.error{role: 'alert'} some alert 
    %p.info{title: 'Important Notification', role: 'status'} Some information
  -else
   %p.info{title: 'Important Notification', role: 'status'} Some information
    %section.form
      %form.standard{action: root_path, method: 'get'}
        %fieldset.search{'aria-labelledby' => 'content-header'}
          %input{type: 'search', name: 'name', role: 'textbox'}
        %fieldset.input-actions
          %input.primary-action{type: 'submit', value: 'search', name: 'invokeSearch'}
    -if @accounts.blank?
      %h3 #{'Result Not Found'}
    -else
      = render 'some_path/path'

我在我的控制器中有以下检查

 def index

       @invalid = !!(params[:name] =~ /[=,\/\\]/)

       if @invalid
          redirect_to index_path
       end

我不确定我在这里做错了什么。我想在顶部显示错误并重新发布到同一页面。

1 个答案:

答案 0 :(得分:1)

简单。

 def index

   @invalid = !!(params[:name] =~ /[=,\/\\]/)

   if @invalid
      flash[:error] = 'Invalid name.'
      redirect_to index_path
   end

flash [:error]将在索引页面上提供。

相关问题