0

In my controller I coded like this

 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

and in my view new.html.haml file

- 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'

In my index.html.haml file I have this

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

The problem is when I click upload button its not render to index page from create

Here I got the log like this

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)

How can I render to index action and view index file content.

Selvamani
  • 7,434
  • 4
  • 32
  • 43
  • 1
    Try changing your render to redirect_to in your create method. – Pavan Feb 04 '14 at 08:48
  • 1
    just wondering, why is it that you don't want to redirect back to index page after create action finish ? `redirect_to company_upload_requests_path` – equivalent8 Feb 04 '14 at 08:50

2 Answers2

1

Use like this.

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

No need to render index action in index response.

def create
  puts params
  respond_to do |format|
    format.html { render "index" }
  end
end
Dheer
  • 773
  • 1
  • 6
  • 16
  • Normal index action is working or not..? for index action you need instance variable (like an array) to fetch records before respond – Dheer Feb 04 '14 at 10:14
  • yeah Normal index action is working. I tried lot of method from here http://stackoverflow.com/questions/5045222/difference-between-render-action-and-render-template – Selvamani Feb 04 '14 at 10:15
  • For index action we need collection of records to show in index page. – Dheer Feb 04 '14 at 10:17
0

Change the render in your create method to redirect_to

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

For more explanation on render vs redirect_to, see this SO Question or this.

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76