RailsCasts Pro episodes are now free!

Learn more or hide this

LucasCioffi's Profile

GitHub User: LucasCioffi

Comments by

Avatar

Yes, you can have one app so people only have to log in once to either system. You can set current_school just like you set current_user with something like the following if you save siteA.com and siteB.com as the domain for each school.

ruby
def current_school
    @current_school ||= School.find_by_domain(request.host)
end

Then you could use @current_school for scoping as mentioned in the episode.

Avatar

Update: To make this work in XLS format, we need to pass in the tab character, so I modified my controller to look like this:

ruby
@products = Product.find_all_by_color("green")
respond_to do |format|
  format.csv { send_data Product.to_csv({},@products) }
  format.xls { send_data Product.to_csv({col_sep: "\t"}, @products) }
end

And then this is how product.rb receives the call of .to_csv with the tab character and @products passed in as variables from the controller:

  def self.to_csv(options,products)
    CSV.generate(options) do |csv|
      csv << column_names
      products.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end
Avatar

Great gem.

Params seem very helpful. From the controller, is it possible to filter public activity by params (for example, storing the recipe in the params and only showing public activity for a recipe on that recipe's page)?

I see in the episode that Ryan filtered directly by one of the attributes (owner_id):

ruby
def index
  @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.friend_ids, owner_type: "User")
end

I see that I can store the recipe_id from within the recipe controller similar to what was mentioned in the episode:

ruby
@recipe.create_activity :create, owner: current_user, :params => {:recipe_id => @recipe.id}

I see that it's possible to filter this way in the view (replacing "123" below with the particular recipe's id, but I'm assuming it's better to do this in the controller, yes?

ruby
<% if activity.trackable && activity.parameters[:recipe_id] == 123 %>
        <%= activity.inspect %><br>
<% end %>
Avatar

I got the same "can't convert Array into Hash" error from

products_controller.rb
format.csv { send_data Product.to_csv(@products) }

but it seems to work if you pass @products as a variable into the model by adding it in the parentheses and in the .each loop as I'm passing all_products in below.

product.rb
def self.to_csv(all_products)
  CSV.generate do |csv|
    csv << column_names
    all_products.each do |product|
      csv << user.attributes.values_at(*column_names)
    end
  end
end

The advantage is that you can define @products in the controller in order to only get the products you want such as in this example:

products_controller.rb
@products = Product.find_all_by_color("green")
respond_to do |format|
  format.csv { send_data User.to_csv(@products) }
end      
Avatar

Great job in this railscast!

I was hoping that Facebook lets a user export their friends' email addresses to external apps so that a user can see which of his/her other friends are already using that app. Facebook doesn't allow this, for better or worse (probably to maintain control and to prevent spam).

The above functionality of helping a user find their Facebook friends on your app can still be accomplished, but it requires that both users go through the trouble of connecting their Facebook accounts to your app. This is a significant barrier, I believe.

Fortunately, I found that OmniContacts enables a user to import their email contacts directly to your app: https://github.com/Diego81/omnicontacts/

Avatar

Using Rails 3.2.3, I did have this error but it is fixed (see below for changes):

You can see I was using different variable names than the example, but hopefully the comparison between the two code snippets below will indicate what might be worth changing in your code.

ERROR
undefined method `klass' for nil:NilClass

BEFORE CHANGES

ruby
<%= user_form.fields_for :aois do |aoi_form| %>
  <%= render 'aoi_fields', f: aoi_form %>
  <%= link_to_add_aois "New AOI", aoi_form, :aoi %>
<% end %>

AFTER 2 CHANGES (no more errors)

ruby
<%= user_form.fields_for :aois do |aoi_form| %>
  <%= render 'aoi_fields', f: aoi_form %>
<% end %>
<%= link_to_add_aois "New AOI", user_form, :aois %>

I hope that is helpful to some folks.

Thanks for these truly awesome tutorials.