Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Reb Con
Reb Con
4,607 Points

Got a problem with devise, attr_accessible and rails 4

Hi guys, I am new here and currently stuck at the authentication part in the rails tutorial. I am using rails 4. I am at "Generating the devise views" and suppose to add :first_name, :last_name, :profile_name to attr_accessible in the model/user.rb file. But the attr_accessible doesn´t exist unlike in the tutorial. It seems the attr_accessible was taken out of Active Model and put into a gem, for security reason in rails 4!? So at this point I am not not sure what to do instead. any help?

8 Answers

The general idea is that allowing these parameters was moved in rails4 from the model to the controller. The video was shot when it was still done in the model (with attr_accessible). There are two ways to do it in rails4: by subclassing devise classes and "the lazy way". They are described in the docs: https://github.com/plataformatec/devise/tree/rails4#strong-parameters and here's a discussion about how to do it in "treebook": https://teamtreehouse.com/forum/strongparameters-and-treebook

Here is some coffescript from railscasts which I found fixes this problem

ready = ->
   $('.status').hover (event) ->
    $(this).toggleClass("hover")

$(document).ready(ready)
$(document).on('page:load', ready
Reb Con
Reb Con
4,607 Points

Looks like there is a ")" missing at the end of ", ready" but wohooo, I will use this now. thanks a bunch.

Hi Rebecca, were you able to figure this out? I'm stuck on the same part... Also after installing all the devise stuff my roll overs no longer work to show edit/delete... can't for the life of me figure this out either. Did you experience the same problem?

Reb Con
Reb Con
4,607 Points

I think I could figure it out (for the moment at least). see my reply to Stone Preston in this thread( I posted a link there).

And about the roll over effect: the links are showing in general when I mouse over, but if I do edit a user, hit save and go back to main statuses page, the edit|delete links won´t show up at all until I reload. Did you mean that?

Hmm.. yeah that's exactly what is happening to me as well. I wonder if anyone knows how to fix this? Treehouse??

Ok, I did some digging. Looks like it is "Turbolinks" that is included in the newest version that is stopping this from working. A simple fix to make this work is here: http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4

Reb Con
Reb Con
4,607 Points

That worked well. Thanks!

No problem!

The problem is that the coffeescript in the tutorial in statuses.js.coffee is broken in rails4 because of turbolinks. But you do not have to disable turbolinks. You just have to fix the tutorial... I do not know enough coffeescript so I will provide the javascript solution below.

First, the coffeescript is equivalent to the following javascript (you need to change statuses.js.coffee to statuses.js):

statusToggler = function() {
    return $('.status').hover(function(event) {
        return $(this).toggleClass("hover");
    });
};
// call statusToggler when the document is ready:
$(statusToggler);

Notice that when you call statusToggler it looks for all elements with class="status" attributes, and listen to hover events on them. Then, when an hover event fires on any of these elements, it toggles (adds or removes) the class="hover" attribute. Recall that the CSS (in statuses.css.scss) has different styles (hiding vs. inline) for status elements with and without class="hover", so this achieves the hide/show effect.

So what is broken? Well, statusToggler is still ok, but turbolinks does not reload pages, so the statusToggler is never called again (except after the first time). This means that nothing is listening to hover events on your second page, so you do not get the hovering effect.

How to fix this? According to the turbolinks docs, it fires a "page:load" event when it loads a new page. So all we need to do is add:

document.addEventListener("page:load", statusToggler);

To summarize, you do not need to disable turbolinks- instead you need to add an event that listens to when turbolinks load the next pages. So if you replace the statuses.js.coffee from the tutorial with the following status.js you will fix the problem:

statusToggler = function() {
    return $('.status').hover(function(event) {
        return $(this).toggleClass("hover");
    });
};
// call statusToggler when the document is ready:
$(statusToggler);
document.addEventListener("page:load", statusToggler);
Reb Con
Reb Con
4,607 Points

George, awesome, thank you (also for the explanation). Just tried this and it works fine.

Reb Con
Reb Con
4,607 Points

thank you, I will try that!