RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Interesting but I don't like this approach, works well for simple applications, but it's very open to attacks, I think a better approach is using PostgreSQL schemas, and changing the schema with the subdomain, this way you truly create a separate schema (DB) with it's own data, of course when you should create a new schema for each new subdomain, and you should run the migrations in all schemas.

Avatar

Hello guys,

I'm getting the following error:

LoadError in SessionsController#create

Expected /Users/jeanosorio/rails_projects/andinitos/app/models/user.rb to define User

Does anybody has an idea, what is the problem???

Thanks

Avatar

I'm also looking for this.. Does anyone did this?
Thanks!!

Avatar

You could include the things that can change in the cache key, like <% cache [post, user.admin?] do %>, but then you lose some of the automatic cache invalidation that cache digests provide when nesting cache fragments.

Avatar

The SVN blog discusses that. The short answer is you can't use conditionals that can change when the template itself doesn't. Impacts system design a bit.

Avatar

This may be a newbie question but here goes...
Suppose my data is date based. (Like events on a calendar), and I want my "pages" to correspond to some units of time. For example, some number of weeks.
So that page 1 is events occurring in next 2 weeks from today... and Page 2 is events occurring in the next 2 weeks.... etc.
In this case, a page isn't a fixed number of items. It may be many or few.

In this use case, is pagination via kaminari something I should be using, or should I be hacking together something specific to my scenario? And if so, does anyone have any pointers to where to go for advice?

Thanks,
John

Avatar

I have tried to follow this tutorial to implement it in my own project, but I am never able to get it work. After adding the new.js.erb and adding the js code shown in the video, when I click 'Add Post' link nothing happens. I have followed the trouble shooting steps recommended by Ryan, still no dice. Although, I did get a n/w status of 304 unmodified as opposed to the 200 ok that Ryan got in the video. Any ideas as to what I am doing wrong?

Avatar

+1 for testing... if not talk about it, include some example tests in the episode source code!

Avatar

Why not putting

ruby
default_scope { where(tenant_id: Tenant.current_id) }

in a Concern and include that module in models?

Don't you think it would be great to talk about testing in Pro episods?

Thanks

Avatar

Would there be any gotchas when doing this with sensitive links that show up or remain hidden depending if a user is an admin or not?

Avatar

I would like to add a 'Copy' button that creates a new copy of the question with the same set of answers. I think it will be similar to the link_to_add_fields, but I cannot figure out how to capture user input and create a new nested object with those values on the fly. Any help will be appreciated. Thanks.

Avatar

I have the following code

<%= bestin_place comment, :description, :type => :textarea,:ok_button => "Save", :cancel_button => "Cancel",:activator => "#edit#{comment.id}" %>
<%= buttontag "Edit" , :id => "edit#{comment.id}" %>

I have multiple of these in my view.

Clicking on the "Edit" button, in my case, the first time does not activate the editing
I have to click on the description text once and then click on the "Edit" button to activate the editing.

Avatar

So that's what 37signals calls Russian Doll Caching?

Avatar

Does anyone know if Mongoid can be used instead of activerecord?

Avatar

What I'm really having a problem with is embeds_one associations. Can't find any tutorials or docs on it. I can find enough on embeds_many but not what I need.

Anyone have a lead?

Avatar

I found the solution, I have to send the same data sent at checkout, in the create_recurring_profile

Avatar

Thanks! Now, using this method I don't need to define a wrapper class in my application_helper.rb

However, how can I add (say) a CSS class to all forms generated with this form builder?

I tried overriding the "initialize" method and editing the options[:html] hash, but this got me nowhere.

Any ideas?

Avatar

you could also use the figaro gem where you define everything in an yaml file

https://github.com/laserlemon/figaro

Avatar

That is extremely wonderful! Thank you so much for it.

Avatar

I would recommend to use CONFIG.fetch(:key) instead of CONFIG[:key]. If for some reason the :key does not exist an KeyError exception will be raised.

Avatar

Sorry guys but how can I reorganize the fields?
<%= f.fields_for :answers do |builder| %>

That's because I've already done a Drand and Drop ajax order script and now I need to know how to order the fields.

Thanks.

Avatar

Hey if you get: undefined method `reputation_value_for'

change `reputation_value_for' to 'reputation_for'

reputation_value_for was deprecated and replaced by reputation_for, see commit (10/03/2012)

reputation_for(reputation_name, *args)

https://github.com/twitter/activerecord-reputation-system/commit/552c04b0dead76cd79fb3915845b17b6a8de0cca

Also normalized_reputation_value_for was deprecated and replaced by normalized_reputation_for

normalized_reputation_for(reputation_name, *args)

Avatar

There seems to be a syntax error in the ASCIIcast for the ajax search jQuery. Bardwin's coffee-script worked for me however.

Avatar

Is there a way to configure guard to stop running all the features when emacs autosaves a file?

Ex when files beginning with '.#' get saved in the dir being watched;
.#home.feature
home.feature

Avatar

But if you click a button_to so it opens in a new tab it will load a GET request. Tested in Chrome and Safari.

Avatar

Slice is a method added by ActiveSupport to Hash, so if you're not using Rails, I'd expect you'd get a NoMethodError like this.

However, you don't need to use Ryan's snippet to get integration with RSpec metadata--that's built in to VCR 2 now.

Ryan requested this a while ago and I was happy to oblige :).

Avatar

Thanks for the reminder. I've been using "if not" exclusively. I'll try to break this old habit.

Avatar

Ransack gem help for searching data but searchable data is not displayed on PDFs but every-time it displays only the result data got from result method in ransack .When we search data next time and click on PDFs link next time,we get the same previous result but not the searched data.
I am sucked with this problem.
Please guide me.
Thanks and regards.

Avatar

When I submit the form of new subscription, I get this error:

RuntimeError in SubscriptionsController#create

[{:code=>"11581", :messages=>["Invalid Data", "Profile description is invalid"]}]

Application Trace | Framework Trace | Full Trace
app/models/paypal_payment.rb:46:in process'
app/models/paypal_payment.rb:18:in
make_recurring'
app/models/subscription.rb:26:in save_with_paypal_payment'
app/models/subscription.rb:14:in
save_with_payment'
app/controllers/subscriptions_controller.rb:18:in `create'

Can any one help please ?

Avatar

Permissions.permission_for(user) shows nearly what i'm looking for, but it doesn't work for the index action where a the customer_id matches.

  allow_action :cabinets, [ :index, :new, :create, :edit, :update ] do |cabinet|
    cabinet.customer_id == user.customer_id
Avatar

any ideas how to implement the accessible_by for the index action?

like with cancan:
@projects = Project.accessible_by(current_ability)

# This module adds the accessible_by class method to a model. It is included in the model adapters.
# An alternative action can optionally be passed as a second argument.
#
# @articles = Article.accessible_by(current_ability, :update)
#
# Here only the articles which the user can update are returned.

  def accessible_by(ability, action = :index)
    ability.model_adapter(self, action).database_records
  end
Avatar

I like that you can prefix the yaml key with a colon, like so

:symbol:

in your yaml file so you don't have to use symobolize_keys!. Also, if I remember correctly, symbolize_keys! only works one level deep.

Avatar

I think the only performance issue would be load time. Loading the class is probably quicker than parsing the YAML and then loading the relevant data structures. I've never done a compare, but I'd imagine the difference would not matter much.

I just wondered whether setting up a nested data structure is spartan enough in Ruby that the YAML abstraction is often unnecessary.

I like YAML, and when I use Rails, that's how it works, so I use it. But when I've been doing non-rails stuff lately, I've been experimenting with just using Ruby, e.g.:

ruby
module Config

  @ldap_array =     {
        :host => 'server',
        :port => 636,
        :base => '[base]',
        :method => :ssl,
        :bind_dn => "bind_dn",
        :password_block => Proc.new { '[password]' },
        :allow_anonymous => false,
        :try_sasl => false
    }

  def self.ldap_settings
    @ldap_array
  end

end

and then I can use it like:

ruby
class LdapThingee
  include Config

  def some_method()
    LdapPerson.setup_connection(Config.ldap_settings)
  end
end

As best as I can figure, YAML lets you not worry about quoting strings. But the portability of YAML is not really a big selling point for configs in Ruby. In Java, XML was a idea since it meant you could use a standard, portable format, that didn't get compiled into your code. YAML's readability is a big improvement from XML for configs. But since the Ruby doesn't get compiled, why not use it for configs I wondered?

Avatar

ProTip: Don't use "if not," use "unless."

So the authorize method would go from:

def authorize
  if !current_permission.allow?(params[:controller], params[:action])
    redirect_to root_url, alert: "Not authorized."
  end
end

To the more readable:

def authorize
  unless current_permission.allow?(params[:controller], params[:action])
    redirect_to root_url, alert: "Not authorized."
  end
end
Avatar

How would you add a :destroy function to the embedded comments?
Also, any examples on doing this with Carrierwave?

Avatar

Hi I have the same problem here.

How can I implement, that the tag_tokens= runs after_validation?

Avatar

Hi, interesting gem, but can you explain what advantages there are over using the delay extensions available by default?

https://github.com/mperham/sidekiq/wiki/Delayed-Extensions

Avatar

planetmcd comment makes me question if using YAML is the right way to use configurations. The only advantage that I see in YAML is that it's portable. But what about in terms of performance what is better YAML or ruby?

Avatar

I'm a little confused about these line:

ruby
  old_pid = "#{server.config[:pid]}.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid 

Wouldn't that set the old_pid variable to the filename of the unicorn.pid.oldpid file, and then compare that filename with the pid of the new server? For example, it would expand to:

ruby
if File.exists?('/tmp/unicorn.pid.oldpid') && 13274 != '/tmp/unicorn.pid.oldpid'

Or am I misunderstanding something?

Avatar

There seems to be no #slice method available on the example.metadata object.

ruby
undefined method `slice' for #<RSpec::Core::Metadata:0x007f9ee42ab770>
Avatar

Agreed about Devise. It gave me a super quick setup that I almost immediately needed to modify. I'll be taking a look at Sorcery next.

Avatar

I love Ryan's screencasts. I can't seem to get this working for me, however. The grouped_collection_select works, but my app seems to be ignoring the js.coffee file. I have the include tags in my application.html header, but still, no magic is happening. Does anyone know what I'm doing wrong?

invoices.js.coffee :
jQuery ->
$('#invoice_facility_id').parent().hide()
facilities = $('#invoice_facility_id').html()
console.log(facilities)
$('#invoice_division_id').change ->
division = $('#invoice_division_id :selected').text()
options = $(facilitys).filter("optgroup[label=#{division}]").html()
console.log(options)
if options
$('#invoice_facility_id').html(options)
$('#invoice_facility_id').parent().show()
else
$('#invoice_facility_id').empty()
$('#invoice_facility_id').parent().hide()

_form.html.erb :

<label>Division:<font color="red">*</font></b></label>
<%= f.collection_select :division_id, Division.order(:division), :id, :division, :include_blank => true %>
<label>Facility:</label>
<%= f.grouped_collection_select :facility_id, Division.order(:division), :facilities, :division, :id, :facility, :include_blank => true %>

Avatar

Thanks for the tip on rails_config, it's just what I was looking for over here. The built-in support for local overrides is nice, and more elegant than my current home-baked solution.

Avatar

Only just checked back, and will now be watching Part 2. Can fix up my dodgy fix :)

Avatar

I went without the UI version and it worked. I guess the UI version makes quite a lot of decisions for you.