Songs on Rap Genius have paths like /lyrics/The-notorious-b-i-g-ft-mase-and-puff-daddy/Mo-money-mo-problems which are defined in routes.rb as:
map.song '/lyrics/:artist_slug/:title_slug', :controller => 'songs', :action => 'show'
When I want to generate such a path, I use song_url(:title_slug => song.title_slug, :artist_slug => song.artist_slug). However, I'd much prefer to be able to type song_url(some_song). Is there a way I can make this happen besides defining a helper like:
def x_song_path(song)
song_path(:title_slug => song.title_slug, :artist_slug => song.artist_slug)
end
I have auto registration working with authlogic using gaizka's version of authlogic_openid which I found on Github since pelle's original addition of the feature seemed to cause issues.
http://github.com/gaizka/authlogic%5Fopenid
http://stackoverflow.com/questions/1476953/using-authlogic-to-auto-create-users-bypassing-explicit-user-registeration
This one works fine however when using the auto_register feature it ignores my options for authlogic such as retrieving the email from the openid provider... any ideas what I'm doing wrong?
Hhere's the example:
http://big-glow-mama.heroku.com/
http://github.com/holden/authlogic_openid_selector_example/tree/with-facebook/
You can see the difference if you register vs. login...
#user.rb
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.validate_login_field = false
# optional, but if a user registers by openid, he should at least share his email-address with the app
c.validate_email_field = false
# fetch email by ax
c.openid_required_fields = [:email,"http://axschema.org/contact/email"]
#c.required_fields = ["http://axschema.org/contact/email"]
# fetch email by sreg
#c.optional_fields = ["email"]
end
#private method to deal with emails goes here
end
#UserSession.rb
class UserSession < Authlogic::Session::Base
auto_register
logout_on_timeout true
end
Does anybody know of a plug-and-play login system that supports existing logins like Google and OpenID?
I am looking to implement something similar to that of Stack Overflows.
Thanks!
Googled for this to no avail. Didn't find anything in the API either. I was expecting some kind of class method or configuration option to set it...
So, rather than calling
from "[email protected]"
for every method, it could be called automatically.
Does anyone know how to workarounds Heroku's limitations on writing to the theme files for the Typo blogging platform? I'd like to take advantage of the theming capabilities right from the browser. If any theme's stylesheet is changed I get the error message "Unable to write file" in addition to the fact that any theme being changed to one that is not the default causes the page to lose all styling and is displayed in plain text.
I followed this (awesome) blog entry to get it up and working.
Hi There,
Continuing with my adventure to convert COBOL to a Ruby program, I have to convert a decimal digit to a comp-3/packed decimal format. Anyone know of a simple Ruby script or gem that does this?
Berns
I'm using the friendly_id gem. I also have my routes nested:
# config/routes.rb
map.resources :users do |user|
user.resources :events
end
So I have URLs like /users/nfm/events/birthday-2009.
In my models, I want the event title to be scoped to the username, so that both nfm and mrmagoo can have events birthday-2009 without them being slugged.
# app/models/event.rb
def Event < ActiveRecord::Base
has_friendly_id :title, :use_slug => true, :scope => :user
belongs_to :user
...
end
I'm also using has_friendly_id :username in my User model.
However, in my controller, I'm only pulling out events pertinent to the user who is logged in (current_user):
def EventsController < ApplicationController
def show
@event = current_user.events.find(params[:id])
end
...
end
This doesn't work; I get the error ActiveRecord::RecordNotFound; expected scope but got none.
# This works
@event = current_user.events.find(params[:id], :scope => 'nfm')
# This doesn't work, even though User has_friendly_id, so current_user.to_param _should_ return "nfm"
@event = current_user.events.find(params[:id], :scope => current_user)
# But this does work!
@event = current_user.events.find(params[:id], :scope => current_user.to_param)
SO, why do I need to explicitly specify :scope if I'm restricting it to current_user.events anyway? And why does current_user.to_param need to be called explicitly? Can I override this?
read/write_attribute is a great way to enhance default accessors generated by ActiveRecord. Like this for example:
def price
read_attribute(:price) or "This item is priceless and you are by the way #{User.current.login}"
end
The same however does not seem to be working with associations.
Demonstration:
class Product < ActiveRecord::Base
has_and_belongs_to_many :stores
end
Then
>> a = Product.first
=> #<Product id: 1, name: "awesome product", created_at: "2010-05-07 12:11:00", updated_at: "2010-05-07 12:11:00">
>> a.stores
=> [#<Store id: 1, name: "ikea", created_at: "2010-05-07 12:11:28", updated_at: "2010-05-07 12:11:28">]
>> a.read_attribute(:stores)
=> nil
>>
So, is there some sort of read/write_association? Or, if not, is there a reason not to have one?
I am using Factory Girl but like the machinist syntax. So I wonder, if there is any way creating a named blueprint for class, so that I can have something like that:
User.blueprint(:no_discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader false
name "foolish"
salt "21746899800.223524289203464"
end
User.blueprint(:discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader true
name "deadbeef"
salt "21746899800.223524289203464"
discount_rate { DiscountRate.make(:rate => 20.00) }
end
DiscountRate.blueprint do
rate {10}
not_before ...
not_after ...
end
Is there a way making factory_girl with machinist syntax acting like that? I did not find one. Help appreciated.
Thx in advance
Jason
In linking a sports event to two teams, at first this seemed to make sense:
events
- id:integer
- integer:home_team_id
- integer:away_team_id
teams
- integer:id
- string:name
However I am troubled by how I would link that up in the active record model:
class Event
belongs_to :home_team, :class_name => 'Team', :foreign_key => "home_team_id"
belongs_to :away_team, :class_name => 'Team', :foreign_key => "away_team_id"
end
Is that the best solution?
In an answer to a similar question I was pointed to single table inheritance, and then later found polymorphic associations. Neither of which seemed to fit this association. Perhaps I am looking at this wrong, but I see no need to subclass a team into home and away teams since the distinction is only in where the game is played. If I did go with single table inheritance I wouldn't want each team to belong_to an event so would this work?
# app/models/event.rb
class Event < ActiveRecord::Base
belongs_to :home_team
belongs_to :away_team
end
# app/models/team.rb
class Team < ActiveRecord::Base
has_many :teams
end
# app/models/home_team.rb
class HomeTeam < Team
end
# app/models/away_team.rb
class AwayTeam < Team
end
I thought also about a has_many through association but that seems two much as I will only ever need two teams, but those two teams don't belong to any one event.
event_teams
- integer:event_id
- integer:team_id
- boolean:is_home
Is there a cleaner more semantic way for making these associations in active record? or is one of these solutions the best choice?
Thanks
I am trying to test facebook api calls with cucumber. Here is the code.
# app/controller/facebook_users_controller.rb
class FacebookUsersController < ApplicationController
def create
fb_user = facebook_session.user
user = User.new(:facebook_uid => fb_user.uid, :facebook_session_key => facebook_session.session_key
respond_to do |format|
if user.save
format.json { render :json => { :status => 'ok' }.to_json }
end
end
end
end
# features/steps/facebook_connect_step.rb
Given /^I am a facebook connected user$/ do
mock_session = Facebooker::MockSession.create
post('/facebook_user.json')
puts response.code
end
When I run the cucumber step for above step definition, I get a response code of 406 instead of 200. This happens in the cucumber test environment only and not in the browser(development/production).
I've been having almost the same issues as Victor Martin (you can see the questions asked here).
I've got declarative authorization working for just about everything that doesn't involve using conditionals. E.g.
has_permission_on :users, :to => [:edit, :update, :destroy] do
if_attribute :user => is { current_user }
end
Are there any common pitfalls with Declarative Authorization? I'm using authlogic and I'm suspicious the 'current_user' method in the application controller might be the source of the problem.
I'm wondering if theres a best practice for what I'm trying to accomplish...
First we have the model categories, categories, has_many posts.
Now lets say, users add posts.
Now, I have a page, that I want to display only the current user's posts by category.
Lets say we have the following categories: A, B, and C
User 1, has posted in Categories A and B.
In my view I have something like:
<% @categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% category.posts.select {|post| post.user == current_user}.each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
For the most part its almost there. The issue is, it will still show an empty category if any posts have been entered in it at all (even if the current user has not input posts into that category.
So the question boils down to:
How do I make the following loop only count posts in the category from the current user?
<% @categories.select {|cat| cat.posts.count 0}.each do |category| %
Best,
Elliot
I've got a web app where I'd like to user FTS3 functionality of SQLite3.
I've got the SQLite3-ruby v1.2.5 gem installed. I'd like to have FTS3 support, and I know, due to trying to create an FTS3 table, that it doesn't come with it.
Do I need to compile the gem/sqlite3 myself in order to get this support? Is there a seprate gem I should be using?
I've written this class that returns feed updates, but am thinking it can be further improved. It's not glitchy or anything, but as a new ruby developer, I reckon it's always good to improve :-)
class FeedManager
attr_accessor :feed_object, :update, :new_entries
require 'feedtosis'
def initialize(feed_url)
@feed_object = Feedtosis::Client.new(feed_url)
fetch
end
def fetch
@feed_object.fetch
end
def update
@updates = fetch
end
def updated?
@updates.new_entries.count > 0 ? true : false
end
def new_entries
@updates.new_entries
end
end
As you can see, it's quite simple, but the things I'm seeing that aren't quite right are:
Whenever I call fetch via terminal, it prints a list with the updates, when it's really supposed return an object.
So as an example, in the terminal if I do something like:
client = Feedtosis::Client.new('http://stackoverflow.com/feeds')
result = client.fetch
I then get:
<Curl::Easy http://stackoverflow.com/feeds>
Which is exactly what I'd expect. However, when doing the same thing with "inniting" class with:
FeedManager.new("http://stackoverflow.com/feeds")
I'm getting the object returning as an array with all the items on the feed.
Sure I'm doing something wrong, so any help refactoring this class will he greatly appreciated.
Also, I'd like to see comments about my implementation, as well as any sort of comment to make it better would be welcome.
Thanks in advance
I need to convert a Date object into a TimeWithZone object representing the beginning of that day in a given time zone.
The following approach works, but seems too convoluted as it requires me to convert the date to a string:
?> date = Date.parse("2010-02-17")
=> Wed, 17 Feb 2010
>> ActiveSupport::TimeZone['Eastern Time (US & Canada)'].parse(date.to_s)
=> Wed, 17 Feb 2010 00:00:00 EST -05:00
>> ActiveSupport::TimeZone['UTC'].parse(date.to_s)
=> Wed, 17 Feb 2010 00:00:00 UTC 00:00
Is there a better way I'm missing?
I'm trying to define a step to test the value of alt text of an image using capybara and the css selectors.
I wrote one for input values based on the readme examples:
Then /^I should see a value of "([^\"])" within the "([^\"])" input$/ do |input_value, input_id|
element_value = locate("input##{input_id}").value
element_value.should == input_value
end
But I can't figure this one out...something like:
Then /^I should see the alttext "([^\"]*)"$/ do | alt_text |
element_value = locate("img[alt]").value
Anyone know how I can locate the alt text value?
As in the standart cycle:
- @goods.each do |good|
???
...to organize this (HAML):
.columns-wrapper
.column First good
.column Second good
.column Third good
.columns-wrapper
.column Fourth good
.column Fifth good
.column Sixth good
I have -- what I think -- is a simple question. Here's my code:
class Fruit < ActiveRecord::Base
end
class Apple < Fruit
end
class Kiwi < Fruit
end
Assume that I have all the STI setup correctly, and there are multiple types of Apple and Kiwi records in the table. From here...
fruits = Fruit.find(:all)
...how do I return an array of just Apples from the fruits array?
What is the proper way to register users automatically when they log in with openid?
I am using authlogic with an authlogic-oid gem (and an older version of openid_authentication).
The stuff I read online so far seems to be obsolete.
Does anyone know the proper way to do it with the new gem?
What I do now is:
options = params[:user_session] || {}
[:openid_identifier].each { |k| options[k] = params[k] if params[k] }
@user_session = UserSession.new(options)
@user_session.save do |result|
if result
flash[:notice] = "Login successful! (#{result.inspect})"
redirect_back_or_default account_url
else
render :action => :new
end
end
By the way, I don't see the Login Successful flash (but that is not that big of a deal).
class Question < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :questions
end
When I find some questions, I usually need to get their authors at the same time, so I use:
Question.find(:all, :include=>:authors)
But I don't write the ":include" part everywhere. I hope I can define the "include" somewhere only once, and when I find questions, the author will be automaticly loaded. Is there any way to do this?
Whats the best way to enable users to log in with their email address OR their username? I am using warden + devise for authentication. I think it probably won't be too hard to do it but i guess i need some advice here on where to put all the stuff that is needed. Perhaps devise devise already provides this feature? like in the config/initializers/devise.rb you would write:
config.authentication_keys = [ :email, :username ]
To require both username AND email for signing in. But i really want to have only one field for both username and email and require only one of them. I'll just visualize that with some ASCII art, it should look something like this in the view:
Username or Email:
[____________________]
Password:
[____________________]
[Sign In]
I've set up my app exactly in line with the Railscasts Time Zone Episode 1 but when I run
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>
I get this error
NoMethodError in Users#new
Showing app/views/users/new.html.erb
where line #27 raised:
You have a nil object when you didn't
expect it! You might have expected an
instance of Array. The error occurred
while evaluating nil.<=
With line 27 being the aforementioned line. I am really stuck on this...