Activity
Posted in Ransack Scoping issues
That's all correct except I don't think you need to move the schools_controller.rb because it's a top level controller.
Good luck and fingers crossed it all works! :)
Posted in I'm lost and can't find the way out
Ah, yes. What you really want is a before_validation
rather than before_create
to look up the UUID and then attach it to the model. Then your validation can check to make sure that the UUID is not nil
.
class User
validates :uuid, presence: true
before_validation :set_uuid, unless: :uuid? # Skip the validation if the uuid is already set
def set_uuid
self.uuid = MojangApi.get_profile_from_name(username).uuid
rescue Exception => e# You will need the MojangApi exception thrown when not found
# This can be empty because we just need to make sure it doesn't break
end
end
So this basically would be what you want to only look up the UUID the first time and if it is already set, you can leave it.
Optionally, the only other thing you might want is a hidden field for the UUID in the form if validations fail so that you can submit it the second time without having to do a second lookup.
Posted in Ransack Scoping issues
Yep! Routes are fine.
You want to put the code in the app/controllers/schools
folder so that it can detect it properly as the module. Just create it and store it in there and you'll be fine.
Also you want to have it inherit from ApplicationController because I forgot that part.
Might give this episode on nested routes a rewatch. This is where I touched on how the modules/nesting work for organizing your code into folders.
Posted in Ransack Scoping issues
Gotcha, yeah you would definitely want to separate those then.
So here's how you can set that up (basically nested routes).
resources :schools do
resources :pins, module: :schools
end
This route basically generates /schools/:school_id/pins
(verify that with rake routes
).
That means you can query the school from the URL by doing @school = School.find(params[:school_id])
# app/controllers/schools/pins_controller.rb
class Schools::PinsController
def index
@school = School.find(params[:school_id])
@q = Pin.search(params[:q])
@pins = @q.result(distinct: true).where(school: @school).order("created_at DESC").paginate(:page => params[:page], :per_page => 10 )
respond_with(@pins)
authorize @pins
end
end
I moved the where
into the result line because that's where you're doing the other scopes so it fits a bit better. In general, I think that should work and should separate out things nicely between the schools.
You can also authorize @school
here to verify they can only ever see pins for their school as an added bonus.
Posted in Ransack Scoping issues
It might make sense to have pins nested under the school depending on what you're trying to accomplish. If you're trying to scope the pins to a school, that's what I would do. Then you can grab the school's ID from the url to filter the search.
The other alternative is to put a hidden field in the form to pass in the school_id_eq
parameter in the URL so that pins are filtered by that ID. This makes the pins controller generic, so it can be used for seeing pins from multiple schools.
Posted in Ransack Scoping issues
What's the URL for the Pins index? Is it /schools/:school_id/pins
?
It might be. I didn't see anything requiring you to set it. I just updated the code from the GoRails episode and everything is working fine without the token(and it didn't require me to put the token in the URL).
You're doing this with S3 presigned uploads and that seems to be the piece that's requiring the token?
Posted in Ransack Scoping issues
It looks like the main thing is that you need to set @school
in the action. Once you have that, you can either merge that into the params[:q]
search options or add where(school: @school)
into your query.
Can you access the school off the URL or the current user?
Let me know if that works for you. I'll have to update the video soon.
That's always the hard part of making screencasts for gems like this!
It looks like when the error gets thrown, they give you a random secret key that you should copy paste into an initializer then restart your app and you should be good.
I believe the secret_key
is a new security feature added recently and you probably just need to follow it's instructions. https://github.com/refile/refile#retrieving-files
This wasn't something that existed when I recorded those videos originally.
Posted in I'm lost and can't find the way out
Check this out and see if it helps!
If you've got a field on the User model called minecraft_uuid, when you query for a user, you can pass that attribute into the API.
@user = User.first # query for a single user
profile = MojangApi.get_profile_from_name(@user.username) # this should return the profile
You can then take the profile attributes and save them to the User or something. If you only want this to happen one time, you can make it a before_create
on the User model.
class User < ActiveRecord::Base
validates :username, presence: true
before_create :load_profile
def load_profile
profile = MojangApi.get_profile_from_name(username) # Only use username here because we are inside a User
assign_attributes(uuid: profile.uuid, other_attribute: profile.other_attribute) # Save those attributes to the user
end
end
You can validate no spaces using the format validator. Some examples here: http://stackoverflow.com/questions/18281198/rails-validate-no-white-space-in-user-name
Posted in File Uploads with Refile Discussion
It's on aws-sdk 2 now. https://github.com/refile/r...
Nope, this is mainly just to emulate production on your development machine.
Not really. You'll need to setup load balancers, etc to do that.
Posted in Must Read / Must Watch
Really enjoyed this post for working with software development teams. http://www.defmacro.org/2014/10/03/engman.html
Posted in Exporting Records To CSV Discussion
Since that's not an attribute, you can just swap this out:
csv << attributes.map{ |attr| user.send(attr) }
With something like this:
csv << [user.name, user.email, user.coupon.code]
That way you can just reference any attributes or methods you want when exporting and they don't have to be database attributes.
Posted in Idea for TimeClock Need Advice
I don't think this needs refactoring. It's straightforward and unless it gets more confusing, you won't get any benefit out of changing it. The only suggestion I'd make is to use an else, because you don't need two separate if statements.
<% if @clock_event.clock_in? %>
<%= link_to "Clock Out", clock_out_employees_path(@clock_event), :method => :put %>
<%= else %>
<%= link_to "Clock In", clock_in_employees_path(@clock_event), :method => :put %>
<% end %>
You could move this logic into a helper, but that's up to you. That will only just obscure what's going on a bit and not give you any more clarity when reading this again 3 months from now.
Posted in File Uploads with Refile Discussion
That's a great question. I think Refile might not be ideal, but you can always ask the author on the Github issue tracker.
Another useful resource is: http://bclennox.com/extreme...
Posted in Coffeescript Polling Issues
Ah yep, I overlooked the method call and thought you were just passing the reference. That'd do it!
Good debugging!