【问题标题】:rails render is not working in create actionrails render 在创建操作中不起作用
【发布时间】:2014-02-28 03:11:36
【问题描述】:

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

 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)

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

【问题讨论】:

  • 尝试在 create 方法中将渲染更改为 redirect_to。
  • 只是想知道,为什么您不想在创建操作完成后重定向回索引页面? redirect_to company_upload_requests_path

标签: ruby-on-rails


【解决方案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

【讨论】:

  • 正常索引操作是否有效..?对于索引操作,您需要实例变量(如数组)在响应之前获取记录
  • 是的 正常索引操作正在运行。我从这里尝试了很多方法stackoverflow.com/questions/5045222/…
  • 对于索引操作,我们需要收集记录以显示在索引页面中。
【解决方案2】:

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

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

有关render 与redirect_to 的更多解释,请参阅此SO 问题或this.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多