Steve

Joined

390 Experience
1 Lesson Completed
0 Questions Solved

Activity

Posted in Bundle Install Doesn't Work

Hi guys,

I seem to be getting an error message when I run bundle install. I'm curious if any of you have had this issue before as well? I tried uninstalling bundler and reinstalling it. I tried different versions of ruby and rails. Any guidance would be appreciated it.

It also says "Unfortunately, an unexpected error occurred, and Bundler cannot continue."

Errno::EIO: Input/output error - sendfile
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1394:in copy_stream'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1394:in
block (2 levels) in copy_file'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1393:in open'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1393:in
block in copy_file'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1392:in open'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1392:in
copy_file'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:486:in copy_file'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:403:in
block in cp'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1571:in block in fu_each_src_dest'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1587:in
fu_each_src_dest0'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:1569:in fu_each_src_dest'
/home/stephen/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/fileutils.rb:402:in
cp'

Thanks,

Steve

You're a genius! Thank you so much.

I can't seem to get any of my nested attributes like "title", "description", or "price." I'm hoping to export the file and then see the nested data that I inputed in the CSV file. To use the example above, I should see "20inch Monitor" for the title. I'm not receiving anything. Basically the CSV file is blank. I see the headings for title and all, but it's not populating the entries.

oh sorry, not sure why it did that.

Product id: 3, sku: "1234", title: "20inch Monitor", description: "used", price: "60", quantity: 2, invoice_id: 3, created_at: "2017-08-22 12:37:21", updated_at: "2017-08-22 12:37:21"

When I just run Product, it says:
"Product (call 'Product.connection' to establish a connection)"

When I hit Product.last, it does output an entry.

=> #

I'm not sure if you can see this, but this is my stack trace.

https://www.dropbox.com/s/s74aji1vg3ugjko/error.jpg?dl=0

Hi John,

When I put the code you gave me. The error I get is like this (assuming this is the stack trace):

NoMethodError in InvoicesController#index

  all.each do |product|
        Rails.logger.debug "Last Product: #{product.id}"
        ***  Rails.logger.debug "Last Product: #{product.title}"***
        csv << product.attributes.merge(product.title.attributes).values_at(*attributes)
  end
end

The attributes for products are sku, title, price, description, and quantity. In my product.rb model page, I got belongs_to :invoice. As follows:

class Product < ApplicationRecord
  belongs_to :invoice

end

invoice.rb


class Invoice < ApplicationRecord
  has_many :products, dependent: :destroy

  def self.to_csv
    CSV.generate(headers: true) do |csv|
      csv << attributes = %w{date vendor_name invoice_number title  }

      all.each do |product|
            Rails.logger.debug "Last Product: #{product.id}"
              Rails.logger.debug "Last Product: #{product.title}"
            csv << product.attributes.merge(product.title.attributes).values_at(*attributes)
      end
    end
  end

invoices_controller.rb

  def index
    @invoices = Invoice.order(created_at: :desc)

    respond_to do |format|
      format.html
      format.csv { send_data @invoices.to_csv, filename: "users-#{Date.today}.csv" }
    end
  end

I think you're right. It's probably the case I'm not associating it. I'm just not sure what's wrong though. I've tried to fiddle with the controller, but no luck. Thanks again.

Steve

Hi John,

Many thanks. I'm somehow still getting an error that says "undefined method `title' for "

  def self.to_csv

    CSV.generate(headers: true) do |csv|
      csv << attributes = %w{date vendor_name invoice_number title  }

      all.each do |product|
            csv << product.attributes.merge(product.title.attributes).values_at(*attributes)
      end
    end
 end

I really appreciate your detailed explanation on how ActiveRecord works. It's very clear.

Steve

Hi Jack,

Thank you for your response. Your idea is a good one, but I'm having problems calling the method. I'm assuming I put the method definition in the invoice controller, right? It doesn't seem to populate the product data.

Steve

Greetings:

I would like to export a CSV file where it includes all the nested attributes. I have an invoice model and a product model. Each invoice has many products. I am able to export to CSV following the GoRails video, but how do I include the data from the products page?

I'm fairly new to Rails so any help would be appreciated.

Many thanks,

Steve

Here's my code

class Invoice < ApplicationRecord
        has_many :products, dependent: :destroy

        def self.to_csv
            attributes = %w{Date Vendor_name Invoice_number}
            CSV.generate(headers: true) do |csv|
                csv << product.attributes.values

                all.each do |product|
                    csv << product.attributes.values_at(*attributes)
                end
            end
        end

end
<%= link_to "Export to CSV", invoices_path(@invoices, format: :csv), class: 'btn btn-info' %>
  def index
    @invoices = Invoice.all

    respond_to do |format|
      format.html
      format.csv { send_data @invoices.to_csv, filename: "users-#{Date.today}.csv" }
    end
  end

I would like some help on how to build a dynamic dropdown menu with Rails Simple form.

I'm really new to programming so any help will be appreciated.

Many Thanks,

SS

Here is my Simple Form code.


  <%= f.input :country :  collection: ["Canada", "United States"] %>
  <%= f.button :submit %>

Here's is some code in HTML and JS. This works, but I was wondering if Rails can do something similar.


  <script>
    function select_countries(selectElem) {
      var regionCode = selectElem.options[selectElem.selectedIndex].value;

      if (regionCode == 'us') {
        document.getElementById("us_region").style.display = 'block';
        document.getElementById("ca_region").style.display = 'none';
      } else if (regionCode == 'can') {
        document.getElementById("ca_region").style.display = 'block';
        document.getElementById("us_region").style.display = 'none';
      }
    }

      function select_region(selectElem) {
        var cityCode = selectElem.options[selectElem.selectedIndex].value;

        if (cityCode == "on") {
            document.getElementById("on_cities").style.display = 'block';
            document.getElementById("qc_cities").style.display = 'none';

          } else if (cityCode == "qc") {
            document.getElementById("qc_cities").style.display = 'block';
            document.getElementById("on_cities").style.display = 'none';

          } else if (cityCode == "ny") {
            document.getElementById("ny_cities").style.display = 'block';
            document.getElementById("ca_cities").style.display = 'none';
          } else if (cityCode == "ca") {
            document.getElementById("ca_cities").style.display = 'block';
            document.getElementById("ny_cities").style.display = 'none';
          }
        }

  </script>

  <body>

      <form name="form1" method="post" action="">
          <p>
          <label for="countries">Countries:</label>
          <select id="countries" name="countries" onChange="select_countries(this);">
          <option disabled selected value>No Country Selected</option>
          <option value="can">Canada</option>
          <option value="us">United States</option>
          </select>
          </p>

          <p id="ca_region">
          <label for="provinces">Provinces:</label>
          <select id="provinces" name="provinces" onChange='select_region(this);'>
          <option disabled selected value>No Province Selected</option>
          <option value="on">Ontario</option>
          <option value="qc">Quebec</option>
          </select>
          </p>

          <p id="us_region">
          <label for="states">States:</label>
          <select id="states" name="states" onChange='select_region(this);'>
          <option disabled selected value>No State Selected</option>
          <option value="ny">New York</option>
          <option value="ca">California</option>
          </select>
          </p>

          <p class="cities" id="on_cities">
          <label for="on_cities">Cities:</label>
          <select id="on_cities" name="on_cities" onChange='select_cities("on_cities");'>
          <option value="hamilton">Hamilton</option>
          <option value="toronto">Toronto</option>
          </select>
          </p>

          <p class="cities" id="qc_cities">
          <label for="qc_cities">Cities:</label>
          <select id="qc_cities" name="qc_cities" onChange='select_cities("qc_cities");'>
          <option value="montreal">Montreal</option>
          <option value="quebec">Quebec City</option>
          </select>
          </p>

          <p class="cities" id="ny_cities">
          <label for="ny_cities">Cities:</label>
          <select id="ny_cities" name="ny_cities" onChange='select_cities("ny_cities");'>
          <option value="newyork">New York City</option>
          <option value="albany">Albany</option>
          </select>
          </p>

          <p class="cities" id="ca_cities">
          <label for="ca_cities">Cities:</label>
          <select id="ca_cities" name="ca_cities" onChange='select_cities("ca_cities");'>
          <option value="losangeles">Los Angeles</option>
          <option value="sanfrancisco">San Francisco</option>
          </select>
          </p>

        </form>

I appreciate the response guys. Yes, I would like each row to have an S auto populated.

I'm very new at programming so just bare with me. Would I need to create two def methods with each params[:set]? like this?

  def download_csv
    respond_to do |format|
      format.html
      format.csv { send_data Post.all_entries_csv(params[:set2]) }
    end
  end

Many thanks guys!

Hello,

I have two questions: 1) I would like to create a button that can generate a CSV file with one set of attributes, and another button that will be able to generate a different set of attributes.

2) Is it possible to add a feature where the CSV file will auto-populate an attribute(or heading) with line-items filled in. For example, when I generate the CSV file I want the sales_tax heading with a code "S" auto-populated in it.

I was able to follow the "Import and Export CSV file" series successfully, but it didn't have this feature I'm looking for.

Thanks in advance.

Steve

#Controller 
class UsersController < ApplicationController
  def index
    @users = User.all

    respond_to do |format|
      format.html
      format.csv { send_data @users.to_csv, filename: "users-#{Date.today}.csv" }
    end
  end
 #Model     
    class User < ApplicationRecord
  def self.to_csv
    attributes = %w{id email name price quantity sales_tax total_amount }

    CSV.generate(headers: true) do |csv|
      csv << attributes

      all.each do |user|
        csv << user.attributes.values_at(*attributes)
      end
    end
  end

views section
<%= link_to 'csv1', users_path(format: "csv") %>

<%= link_to 'csv2', users_path(format: "csv") %>