rails render在create action中不起作用

时间:2014-02-04 08:41:13

标签: ruby-on-rails

在我的控制器中,我这样编码

 class CompanyUploadRequestsController < ApiController
  def index
    respond_to do |format|
      format.html { render action: "index" }
    end
  end

  def create
    puts params
    respond_to do |format|
      format.html { render action: "index" }
    end
  end

  def new
  end

end

并在我看来new.html.haml档案

- page_title("Upload Company")
%h1 Upload Company
%hr
#upload-form
  = simple_form_for(@company_upload, :as => :company_update, :url => company_upload_requests_path(@company_upload), :html => { :class => 'file-style'}) do |f|
    = f.error_notification

    .form-inputs
      = f.input :requestname, :label => false, :id => "request_name_input"
      = f.input :file,:as => :file, :label => false, :id => "file_select_input"

    .form-actions
      != link_to 'Cancel', company_upload_requests_path, :class => 'btn'
      = f.button :submit, 'Upload', :class => 'btn-primary'

在我的index.html.haml文件中,我有这个

- page_title("Upload Company")
%h1 Company index
= link_to("Upload File", new_company_upload_request_path, :class => "btn btn-primary pull-right")

问题是当我点击上传按钮时,它不会从创建

渲染到索引页面

我在这里得到了这样的日志

Processing by CompanyUploadRequestsController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"oygIP62E4GHefhN9OnvB3sKhddIb4CN/izfvF5GQtuI=", "company_update"=>{"requestname"=>""}, "commit"=>"Upload"}
  Rendered company_upload_requests/create.html.haml within layouts/application (9.8ms)

如何呈现索引操作并查看索引文件内容。

2 个答案:

答案 0 :(得分:1)

像这样使用。

def index
   @company_uploads = ModelName.all
   respond_to do |format|
     format.html 
   end
end

无需在索引响应中呈现索引操作。

def create
  puts params
  respond_to do |format|
    format.html { render "index" }
  end
end

答案 1 :(得分:0)

将创建方法中的render更改为redirect_to

def create
    puts params
    respond_to do |format|
      format.html { redirect_to action: "index" }
    end
  end

有关renderredirect_to的更多说明,请参阅此SO问题或this.