I'm new to rails. Im developing an app which contains adding customers.In that I have download button.When I click download button It should download the current customer page in csv file.
Controller
def create
@customer_detail = CustomerDetail.new(customer_detail_params)
@customer_detail.company_profile_id = current_user.company_profile.id
respond_to do |format|
if @customer_detail.save
format.html { redirect_to edit_customer_detail_path(@customer_detail), notice: 'customerDetails was successfully created.' }
# format.html { render 'edit', notice: 'customerDetails was successfully created.' }
else
format.html { render :new }
end
end
end
def index
@customer_details = CustomerDetail.all
end
def destroy
end
def update
respond_to do |format|
format.html
format.csv { render text: @customer_details.to_csv }
if @customer_detail.update(customer_detail_params)
format.html { redirect_to @customer_detail, notice: 'customer_details was successfully updated.' }
else
format.html { render :edit }
end
end
end
View
.fieldset
.row
.col-sm-3
= f.submit "Save", class: "btn btn-primary"
.col-sm-3
= f.submit "cancel", type: :reset, class: "btn btn-primary"
.col-sm-3
= link_to "Download", edit_customer_detail(format: "csv"), class: "btn btn-primary"
.col-sm-3
= link_to("Print", "javascript:print()", class: "btn btn-primary")
The problem is it downloads all the records from of the form. I dont know whether to action is to be given in update or edit.If i give the path edit_customer_detail instead of customer_details_(path) it shows template error and no route matches error when clicking download button.can someone please help me.I've attached the output link here. thanks in advance!!
model
def to_csv
CSV.generate do |csv|
csv << self.class.column_names
csv << self.attributes.values_at(*self.class.column_names)
end
end
controller
def download_csv
respond_to do |format|
format.html
format.csv { render text: @customer_detail.to_csv }
end
end
view
.col-sm-3
- if @customer_detail.save
= link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments