Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Serguei Cambour's Profile

GitHub User: Javix

Comments by Serguei Cambour

Avatar

Something like this worked for me:

def initialize(user)
    allow :home, :index
...
allow "devise/sessions", [:new, :create]
    allow "devise/registrations", [:new, :create, :destroy]      
    allow_param :user, [:name, :email, :password, :password_confirmation, :remember_me]
if user
        allow "devise/registrations", [:edit, :update, :cancel]
        allow "devise/sessions", [:destroy]
...
allow_all if user.admin?
end

Hope this helps

Avatar

How is it possible to use that with nested attributes (forms). I have my models defined as follows:

class Timesheet < ActiveRecord::Base
  belongs_to :user
  has_many :activities, dependent: :destroy, inverse_of: :timesheet
  has_many :time_entries, through: :activities
  accepts_nested_attributes_for :activities, allow_destroy: true
end

class Activity < ActiveRecord::Base
  belongs_to :timesheet, inverse_of: :activities
  belongs_to :task
  has_many :time_entries, order: :workdate, dependent: :destroy, inverse_of: :activity
  accepts_nested_attributes_for :time_entries, allow_destroy: true, reject_if: proc { |a| a[:worktime].blank? }
end


class TimeEntry < ActiveRecord::Base
  belongs_to :activity, :inverse_of => :time_entries
  validates :worktime, presence: true, inclusion: { in: [0.5, 1] }       
  validates_presence_of :activity     
end

Actually it does not work. As far I could see at Ryan's strong_parameters rails casts github https://github.com/railscasts/371-strong-parameters, he managed to do that (not very dynamically as he said and only for one post). In my case I have 1 or more activity with 7 time_entries each. Any idea how to do that? Thank you.

Avatar

Fails if there is more than one nested resource. In my case I have: Timesheet-> accepts_nested_attributes_for :activities, Activity -> accepts_nested_attributes_for :time_entries. In permission file I declared it as follows:

        allow_param :timesheet, [:status, :user_id, :start_date, :end_date] 
        allow_nested_param :timesheet, :activities_attributes, [:task_id]
        allow_nested_param :activity, :time_entries_attributes, [:workdate, :worktime]

Nevertheless, I always get the error that time_entries attributes is empty. Any idea ? Thanks and regards

Avatar

I followed by letter the screencast tuto but got the below errors:

serge@serge-laptop:~/Development/Ruby/test_projects/blog$ guard
Please install libnotify gem for Linux notification support and add it to your Gemfile
Guard is now watching at '/home/serge/Development/Ruby/test_projects/blog'
Starting Spork for RSpec & Cucumber 
Using RSpec
Using Cucumber
Preloading Rails environment
Preloading Rails environment
Loading Spork.prefork block...
Loading Spork.prefork block...
ERROR: Could not start Spork server for RSpec & Cucumber. Make sure you can use it manually first.
Guard::RSpec is running, with RSpec 2!
Running all specs
No DRb server is running. Running in local process instead ...
No examples found.


Finished in 0.71765 seconds
0 examples, 0 failures
Spork is ready and listening on 8989!
Spork is ready and listening on 8990!

Any idea about the error? Thanks

Avatar

Solved. I don't know why after copy-paste it did not work but after creating the same project from scratch everything worked fine. Thanks!

Avatar

Great episode, Ryan, Thanks a lot> I tried it on a Windows machine with some minore modifications(added some specific gem dependencies) and everything worked as needed. Then I copy-pasted the same project on a Linux (Ubuntu 10.04) PC, changed the dependencies in Gemfile for gem 'libnotify', :group => [:test, :development], and the guard fails after running the bundle and 'guard' commandes:
@@
serge@serge-laptop:~/Development/Ruby/Rails3/auth_rememberme_with_tests$ guard
Guard is now watching at '/home/serge/Development/Ruby/Rails3/auth_rememberme_with_tests'
Guard::RSpec is running, with RSpec 2!
Running all specs
/home/serge/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in load': /home/serge/Development/Ruby/Rails3/auth_rememberme_with_tests/spec/models/users_spec.rb:1: syntax error, unexpected tIDENTIFIER, expecting $end (SyntaxError)
describe "send_pas..."
... ^
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in
block in load'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in load_dependency'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in
load'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in block in load_spec_files'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in
map'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in load_spec_files'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in
run'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in run_in_process'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in
run'
from /home/serge/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
@@@
Any idea on that?
I'm on Rails 3.1. and use Ruby 1.9.2 p0 version.
Thanks a lot.