rails中的form_tag问题

时间:2016-06-13 11:30:49

标签: ruby-on-rails forms ruby-on-rails-3 routes

我在create.html.erb文件中使用form_tag。在这个form_tag下面有一个按钮。现在我希望点击按钮后页面只会刷新,我不想在任何地方重定向。那么我应该在表单标签中写什么作为路径?

对于会话控制器,我的view / sessions / create.html.erb文件是:

<h4>Database contents here:</h4>

<table>                         
  <tr>  
    <th>ID</th>                        
    <th>AGE</th>
    <th>NAME</th>
  </tr>
  <% @data.each do |data| %>       
    <tr>
      <td><%= data.id %></td>  
      <td><%= data.age %></td>  
      <td><%= data.name %></td> 
    </tr>
  <% end %>                        
</table> 

<div>
  <%= form_tag :method => :get do %>
    <%= button_tag type: 'submit'  do %>
      <%= content_tag :div do%>
        <h4>View Data </h4><h4><%= @num %></h4>
     <% end %>
    <% end %>
  <% end %>
</div>

我的contollers / sessions_controller文件是:

class SessionsController < ApplicationController
  def new
  end

  def index
  end

  def create
    user = User.authenticate(params[:email], params[:password])
    @data=Information.all
    @num=Information.count()
    @tym=Time.now.getutc
    if user
      session[:user_id] = user.id
      #redirect_to root_url, :notice => "Logged in!"

      #this part is for showing the content of the information table


      respond_to do |format|
        format.html
        format.xml { render xml: @data }
      end
    else
     flash.now.alert = "Invalid email or password"
     render "new"
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, :notice => "Logged out!"
  end
end

1 个答案:

答案 0 :(得分:0)

如果您只是想刷新页面,那么您不需要执行form_tag,为什么不在按钮点击时重定向回该页面

<%= button_to "Refresh", sessions_path, :method => :get %>