简体   繁体   中英

How to redirect to same page depending on condition in rails

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'

I have the following check in my controller

 def index

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

       if @invalid
          redirect_to index_path
       end

I am not sure if i am doing anythin wrong here. I want to display a error on top and repost to the same page .

Simple.

 def index

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

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

flash[:error] will be available on the index page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM