For uploading an image, the browse button is not properly aligned and also how can i change the colour of the browse button to red? I have used file field.
The config/environment.rb of my rails project contains this line:
RAILS_GEM_VERSION = '>= 2.3.2' unless defined? RAILS_GEM_VERSION
Which makes sure that only Rails of version 2.3.2 or greater will be used to run this app.
Is there a way of specifying both the lower and the upper boundary at the same time? So that it would run, say, only on versions higher than 2.3.1 and lower than 2.3.6?
What is the more "rails-like"? If I want to modify a model's property when it's set, should I do this:
def url=(url)
#remove session id
self[:url] = url.split('?s=')[0]
end
or this?
before_save do |record|
#remove session id
record.url = record.url.split('?s=')[0]
end
Is there any benefit for doing it one way or the other? If so, why? If not, which one is generally more common?
Rake allows for the following syntax:
task :my_task, :arg1, :arg2 do |t, args|
puts "Args were: #{args}"
end
I'd like to be able to do the same, but with RSpecs SpecTask.
The following unfortunately fails:
desc "Run example with argument"
SpecTask.new('my_task'), :datafile do |t, args|
t.spec_files = FileList['cvd*_spec.rb -datafile=#{args}']
t.spec_opts = ["-c -f specdoc"]
end
Is it possible to achieve this with a SpecTask, or is there an alternative approach?
In a standard app, I have this line in my production.rb, which creates endpoints for non-default precompiled assets:
config.assets.precompile += %w( mobile.css )
My rails engine is a standard Sinatra app. It has its own assets.
When on development, these assets are served fine, presumably the web requests are handled by rails and sprockets. On production I'm getting 404s on the assets, and think I have to manually tell sprockets to provide the files. How can this be done without tightly linking?
It isin't evident how to set up env-specific initializers for engines. Is this done? Not only, for example, is config/development.rb within the engine not loaded, but there's no way to get the application class itself without knowing its name, in order to modify configuration.
And even if there was, it seems that having any engine able to reconfigure the main app would be very bad idea.
So maybe its better to let assets handling be done by sinatra itself? Or another instance of sprockets for the engine? How do other engines handle this?
Basically I want to strip the document of words between blockquotes. I'm a regular expression newb and even after using rubular, I'm no closer to the answer.
Any help is appreciated.
I'm using Nokogiri to parse pepXML files from different peptide search engines. I have two pepXML files, both of which appear, inasmuch as I can tell, to be of correct format, and puts Nokogiri::XML(IO.read(file)) will output the whole XML file for both files.
The problem is, doc.xpath("any valid xpath") will parse the tag from one of the files, but not the other. No errors are given, so I have no idea why it won't parse. Anyone know of any reasons why Nokogiri wouldn't parse something out?
I don't think i fully understand character sets so i was wondering if anyone would be kind enough to explain it in layman's terms with examples ( for Dummies).I know there is utf8, latin1, ascii ect
The more answers the better really.
Thank you in advance;-)
I'm undertaking a rather large conversion from a legacy database-driven Windows app to a Rails app. Because of the large number of forms and database tables involved, I want to make sure I've got the right methodology before getting too far.
My chief concern is minimizing the amount of code I have to write. There are many models that interact together, and I want to make sure I'm using them correctly. Here's a simplified set of models:
class Patient < ActiveRecord::Base
has_many :PatientAddresses
has_many :PatientFileStatuses
end
class PatientAddress < ActiveRecord::Base
belongs_to :Patient
end
class PatientFileStatus < ActiveRecord::Base
belongs_to :Patient
end
The controller determines if there's a Patient selected; everything else is based on that.
In the view, I will be needing data from each of these models. But it seems like I have to write an instance variable in my controller for every attribute that I want to use. So I start writing code like this:
@patient = Patient.find(session[:patient])
@patient_addresses = @patient.PatientAddresses
@patient_file_statuses = @patient.PatientFileStatuses
@enrollment_received_when = @patient_file_statuses[0].EnrollmentReceivedWhen
@consent_received = @patient_file_statuses[0].ConsentReceived
@consent_received_when = @patient_file_statuses[0].ConsentReceivedWhen
The first three lines grab the Patient model and its relations. The next three lines are examples of my providing values to the view from one of those relations.
The view has a combination of text fields and select fields to show the data above. For example:
<%= select("patientfilestatus", "ConsentReceived", {"val1"="val1", "val2"="val2", "Written"="Written"}, :include_blank=true )%
<%= calendar_date_select_tag "patient_file_statuses[EnrollmentReceivedWhen]", @enrollment_complete_when, :popup=:force %
(BTW, the select tag isn't really working; I think I have to use collection_select?)
My questions are:
Do I have to manually declare the value of every instance variable in the controller, or can/should I do it within the view?
What is the proper technique for displaying a select tag for data that's not the primary model?
When I go to save changes to this form, will I have to manually pick out the attributes for each model and save them individually? Or is there a way to name the fields such that ActiveRecord does the right thing?
Thanks in advance,
Aaron.
I want to use selenium test to cover my rails project ! but i just find little documents on selenium test . I want someone to give me some documents for selenium test of all types !like website ,pdf ,text etc. you can sent them to my gmail [email protected] Thank you ,and best regards!
I have two Rails applications (lets call them APP-1 and APP-2), each of them has a dependancy on a third Rails application (APP-3).
I would like to be able to run the tests for APP-1 and APP-2 in parallel on my CI server. The problem is, both need to start up APP-3 and write to a DB via the APP-3. This causes conflicts and failures if the tests are run in parallel.
My idea for a solution is for APP-1 and APP-2 to each start their own instance of APP-3 and to have each instance point to a different DB. Is there a way to dynamically set the DB in the database.yml of APP-3 so that it connects to a different DB depending on which APP starts it up?
FYI. APP-1 and APP-2 currently start APP-3 via rake tasks.
Hi, I am trying to get all users that are updated maximum 90 seconds ago:
User.find(:all, :include => { :core => :image },
:conditions => ["updated_at > ?", Time.now - 90.seconds] )
But it doesn't work.
why?
how can i do?
thanks
I'm trying to implement single-column regionalization for a Rails application and I'm running into some major headaches with a complex SQL need. For this system, a region can be represented by a country code (e.g. us) a continent code that is uppercase (e.g. NA) or it can be NULL indicating the "default" information. I need to group these items by some relevant information such as a foreign key (we'll call it external_id).
Given a country and its continent, I need to be able to select only the most specific region available. So if records exist with the country code, I select them. If, not I want a records with the continent code. If not that, I want records with a NULL code so I can receive the default values.
So far I've figured that I may be able to use a generated CASE statement to get an arbitrary sort order. Something like this:
SELECT *, CASE region
WHEN 'us' THEN 1
WHEN 'NA' THEN 2
ELSE 3
END AS region_sort
FROM my_table
WHERE region IN ('us','NA') OR region IS NULL
GROUP BY external_id
ORDER BY region_sort
The problem is that without an aggregate function the actual data returned by the GROUP BY for a given row seems to be untameable. How can I massage this query to make it return only the first record of the region_sort ordered groups?
When signing up for an account on one of my apps, we need to store the time zone is in. We're using the time zone selector, which is fine, but I'd like to set the default value to something that it likely the user's current time zone.
Is there an easy way, either on the server or using JavaScript, to set the time zone selector to the time zone the user is currently in?
I have an array of users that's sorted in descending order based on total_points.
I need to find the rank of each user in that array. The issue is that more than one user can have the same total points and, thus, the same rank. For example, three users could be in 3rd place with 200 Points. Here's my current code:
class Leader < ActiveRecord::Base
def self.points_leaders
all_leaders = all_points_leaders # returns array of users sorted by total_points in desc order
all_leaders_with_rank = []
all_leaders.each do |user|
rank = all_leaders.index(user)+1
all_leaders_with_rank << Ldr.new(rank, user) # Ldr is a Struct
end
return all_leaders_with_rank
end
end
How must I modify the code so that the correct rank is returned, and not just the value of the index position?
Hi, everyone: I am also open to just straight-up refactoring what I'm finding to be pretty repetitive, but to give a baseline of how it's working....
I have for every contact a Campaign, which has_many of three types of Models: Email, Call, and Letter.
When an Email (Call or Letter) has been executed for a specific contact, I have a Contact_Email(_or_Call_or_Letter) which belongs to both the Contact and the Model (Email_or_Call_or_Letter).
Each Contact_Email for example pairing has a :date_sent attribute. So does each Contact_Call and Contact_Letter.
How do I find the latest of all of them?
Here is the code I wrote that can find the latest Email and my finding retyping similar code for Call and Letter, but then stuck on how to do a .max on all of them:
def last_email(contact)
#get campaign the contact belongs to
@campaign = Campaign.find_by_id(contact.campaign_id)
@last_email = ContactEmail.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
@last_call = ContactCall.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
@last_letter = ContactLetter.find(:last,
:conditions => "contact_id = #{contact.id}",
:order => "date_sent DESC")
# how do I get the latest of all of these to display?
if @last_sent_email.nil?
return "no email sent"
else
return @last_sent_email.date_sent
end
end
Question 1: With what I have, how can I find effectively @last_event given I can find the last Email, last Call, and last Letter for every contact?
Question 2: How can I remove the repetitive code that I have to write for each Model?
I'm writing a Sinatra app which needs to render different layouts based on whether the user is using an iPhone or a regular browser. I can detect the browser type using Rack-Mobile-Detect but I'm not sure of the best way to tell Sinatra which layout to use.
Also, I have a feeling that how I choose to do this may also break page caching. Is that true?
Example code:
require 'sinatra/base'
require 'haml'
require 'rack/mobile-detect'
class Orca < Sinatra::Base
use Rack::MobileDetect
helpers do
def choose_layout
if request.env['X_MOBILE_DEVICE'] == :iPhone
# use iPhone layout
else
# use normal layout
end
end
end
before do
# should I use a before filter?
choose_layout()
end
get '/' do
haml :home # with proper layout
end
end #Class Orca
When it comes to remember me cookies, there are 2 distinct approaches:
Hashes
The remember me cookie stores a string that can identify the user (i.e. user ID) and a string that can prove that the identified user is the one it pretends to be - usually a hash based on the user password.
Tokens
The remember me cookie stores a random (meaningless), yet unique string that corresponds with with a record in a tokens table, that stores a user ID.
Which approach is more secure and what are its disadvantages?
I started with:
puts "Hello there, and what's your favorite number?"
favnum = gets.to_i
puts "Your favorite number is #{favnum}?" " A better favorite number is #{favnum + 1}!"
puts "Now, what's your favorite number greater than 10?"
favnumOverTen = gets.to_i
if favnumOverTen < 10
puts "Hey! I said GREATER than 10! Try again buddy."
else
puts "Your favorite number great than 10 is #{favnumOverTen}?"
puts "A bigger and better number over 10 is #{favnumOverTen * 10}!"
puts "It's literally 10 times better!"
end
That worked fine, but if the user entered a number less than 10 the program ended.
I want the user to be prompted to try again until they enter a number greater than 10.
Am I supposed to do that with a loop?
Here's what I took a swing at, but clearly it's wrong:
puts "Hello there, and what's your favorite number?"
favnum = gets.to_i
puts "Your favorite number is #{favnum}?" " A better favorite number is #{favnum + 1}!"
puts "Now, what's your favorite number greater than 10?"
favnumOverTen = gets.to_i
if favnumOverTen < 10
loop.do
puts "Hey! I said GREATER than 10! Try again buddy."
favnumOverTen = gets.to_i
until favnumOverTen > 10
else
puts "Your favorite number great than 10 is #{favnumOverTen}?"
puts "A bigger and better number over 10 is #{favnumOverTen * 10}!"
puts "It's literally 10 times better!"
end
Hi,
I'm looking for suggestions on how to track the number of tags associated with a particular object in Rails. I'm using acts_as_taggable_on and it's working fine. What I would like to be able to do is search for all objects that have no tags, preferably through a scope i.e. Object.untagged.all
My first thought was to use an after_save callback to update an attribute called "taggings_count" in my model:
def update_taggings_count
self.taggings_count = self.tag_list.size
self.save
end
Unfortunately, this does the obvious thing of putting me in an infinite loop. I need to use an after_save callback because the tag_list is not updated until the main object is saved.
Would appreciate any suggestions as I'm on the verge of rolling my own tagging system.
Regards
Robin
I'm using Twitter Bootstrap modal featurs and loading data from remote locations. I'm providing the remote url for a set of thumbnails with the hope that once the thumbnail is clicked, the appropriate data (a large version of the image) is displayed. I'm using the html declarative style to define the remote urls and all the features of the modal.
What I find is that Twitter bootstrap modal loads first remote url then does not display subsequent remote data, (although a request to the proper url is made in Chrome) but displays first loaded data always. How do I get it to show the proper data?
View:
#gallery-navigation
%ul
- @profile.background_images.each do |image|
%li
= link_to image_tag(image.background_image.url(:thumb)), remote_image_path(image.id), :role => "button", :data => {:toggle => "modal", :target => "#image-modal", :remote => remote_image_path(image.id)}, :id => "image-modal"
/ Modal
#image-modal.modal.hide.fade(role="dialog" aria-hidden="true" data-backdrop="true")
.modal-body
Controller:
def remote_image
@image = current_user.profile.background_images.find(params[:image_id])
respond_to do |format|
format.html {
render :partial => "remote_image", :locals => { :image => @image }
}
end
end
when I try and run the "rake db:seed" command the rails console outputs "NoMethodError: undefined method `db' for #" not quite sure what going on. I'm using netbeans to build my rails project which is using the built-in JRuby 1.2 would that have anything to do with it?
Hi,
I'm currently integrating facebook into my current app and I've succeeded in retrieving the access_token using the following code:
url="#{url}?#{client_id}&#{client_secret}&#{code}&#{redirect_uri}&type=client_cred"
agent = Mechanize.new
page = agent.get(url)
The page object above has a body which contains text something along the lines of
access_token=XXXXX
I just want to pull out the access_token value. I can get the entire string simply by writing:
page.body
But I was wondering is there a way to get the access_token value without resorting to regular expressions etc?
Thanks.
Hi,
I am trying to pass a variable to a dynamically declared method like:
eval(def test(name)
puts name
end
test 'joe')
but it does not work.
Is there a way to do this?
In my routes.rb I have this:
map.namespace :admin do |admin|
admin.resources :galleries do |galleries|
galleries.resources :gallery_images, :as=>'images'
end
end
rake routes shows the route created like this:
admin_gallery GET /admin/galleries/:id
and when I go to this url in my browser:
http://192.168.2.2:3000/admin/galleries/11/
I get this error:
Unknown action
No action responded to 11
But I would have expected it to use the show action/view, what am I doing wrong?