Hi,
What is the correct syntax for adding a prompt to a select_tag?
I have this, which works as expected:
%= select_tag author_id, options_from_collection_for_select(@authors,
:id, :name, @selected_author) %
When I try and add a prompt, like this:
%= select_tag author_id,
Have you tried with options?
options = {:prompt = 'Please Select'}
Just did, but didn't work :-(
The effect is unfortunately the same:
select id=author_id name=author_id prompt=Please Select
option value=1Jim/option
...
--
Posted via http://www.ruby-forum.com/.
--
You received this message
Tim Shaffer wrote in post #1041162:
Try this. You can pass a string to include_blank
:include_blank = Please select
Thanks a lot, that works fine!
Out of interest though, this passes author_id in the params as an
empty string, so I have to write:
if params.has_key?(:author_id) and not
Tim Shaffer wrote in post #1041166:
The :prompt option was not added until rails 3.1.0
Now I understand.
I'll go with what you suggested.
Thanks a lot for your help.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups Ruby
Hi,
How would I go about creating a new object with an existing nested
attribute?
class Item ActiveRecord::Base
has_and_belongs_to_many :attachments
accepts_nested_attributes_for :attachments, :allow_destroy = true
end
i = Item.new
= #Item id: nil, name: nil ...
params = {
Hi,
I want to write an application which enables a user to upload a PDF file
to our server.
I would like to develop this application using a test-driven approach
and the Rails default test suite.
However, I am not sure what I need to test and what I don't need to
test.
To elaborate: a user
I have the following associations in my models:
item.rb
belongs_to :manufacturer, :class_name = Company
belongs_to :distributor, :class_name = Company
company.rb
has_many :items
My question: what is the best way to find all of the items belonging to
a specific company?
If I call Company.items
Many thanks for your quick help Colin.
That's a nice solution to the problem and it's also good to find out
what the best practice is.
Jim
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
To post
Although this does indeed work well, I would still be interested to hear
any other suggestions ...
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
To post to this group, send email to
So, no replies to my original question.
In that case I would be grateful if someone could tell me:
Is this a realistic thing to do?
Or, is it a silly idea which goes against some principle of Rails 3?
Thanks
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you
Hi Hassan,
Thanks for your reply.
What happens when you want to add a feature that requires adding
a different gem? Is he going to write his own version of that too? Or
when an existing gem needs an update?
U'm, I'm afraid he might try ...
Install everything, delete what you don't want,
Thank you for trying that out.
That's exactly the error I got.
I'll have a play with it over the next few days and if I figure out a
solution, I'll post back here just to be complete.
Other than that, it seems my best bet is to try to get my colleague to
start installing gems :-)
--
Posted via
Hi,
I'm working on an app for a colleague.
He has written his own pagination class, but it is buggy and not
very DRY. Therefore I replaced it with the well-known will_paginate
gem, and that works just great.
The only problem is, that although this works on my machine, my
colleague (for whatever
I am adding an error to a field in a form thus:
company.errors.add_to_base(Please select either distributor or
manufacturer)
However, the add_to_base method doesn't wrap the field where the error
occurred in a field_with_errors div, and thus the field isn't
highlighted.
Is there any way to
Hi,
I am, trying to install the exception_notification plugin for rails.
I am using rails 2.3, and Windows 7 (64 bit)
The Rails 3 version is here:
https://github.com/rails/exception_notification/
I can install it using git-bash thus:
ruby script/install plugin
Just an update:
I've checked through the documentation for ActionMailer.
Reusing the example they list in their documentation I now have:
def message(sent_at = Time.now)
from 'b...@example.com'
recipients 'j...@example.com'
subject 'New message'
sent_on sent_at
attachment image/jpg do
Hi,
Webrick has stopped giving me any output as to what it is doing.
In the past it would show me what was going on internally as I navigated
around my rails app (e.g it would show me any MySQL queries performed or
if a template was missing).
Now when I start Webrick I get:
D:\Files\Rails
Hi,
I'm trying to use ActionMailer to send an image as an attachment to an
email. Here's my code:
class ContactMailer ActionMailer::Base
def message(sent_at = Time.now)
from 'b...@example.com'
recipients 'j...@example.com'
subject 'New message'
sent_on sent_at
body {}
Content type is JPEG. File you're reading is PNG.
Yeah, I noticed this too, but according to the book I'm reading this
should work.
I tried sending a jpg as an attachment (instead of a png), but with the
same result. I also tried:
attachment :content_type = image/png, :body =
James Byrne wrote in post #975885:
Could you explain the ':' idiom to me? I cannot seem to find any
examples of it by googling.
These two posts were helpful for me:
http://jlaine.net/2008/5/8/amp-lified
http://swaggadocio.com/post/287689063
P.S. The example JLaine uses to illustrate implicit
Hi,
I'm confused as to why this code example (from a book) won't work.
They want to store a price in cents, but be able to set it and query it
in Euros. To this end they are overwriting the getter and setter
methods.
In my model:
class Product ActiveRecord::Base
def price
Cheers Fred,
That was actually embarrassingly obvious.
Thanks for your help.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
To post to this group, send email to
Hi,
I have a flight model.
I want to find all flights with a specific arrival airport, a specific
departure airport and which depart on a certain date.
My problem is that the departure date is a datetime column in the db and
I wish to search this column by date (not datetime).
I have written
Thanks very much for all of the answers.
I went with :departure_datetime = @date...(@date + 1.day) as this was
exactly what I was after.
Thanks also for highlighting the difference between Rails 2 and Rails 3.
I look forward to giving Rails 3 a try.
Best,
Jim
--
Posted via
Hi,
I'm trying to understand some code in a book I'm reading and was hoping
someone could tell me if I have understood it correctly.
There are two models, an employee model and a department model. A
department has_many :employees and an employee belongs_to :department.
Here's a snippet of code
Thanks for the answer Philip. It is good to get confirmation
that I have understood things correctly.
Yes. And it only does this if the update to @employee was successful
first.
Is the order particularly significant in this case (i.e. that it
attempts to update @employee first)?
Obviously it
No technical reason (as this is an update, not a create), but logically
it would make sense that you'd want to update them in that order.
Cool. That was exactly the conclusion I'd drawn.
won't rails throw an error and re-render the 'edit' view if validation
for either @employee or
Hi Bente,
Thanks for your reply.
You should use
accepts_nested_attributes_for :department
in your employee model.
The code you provided needed a minor tweak:
% form.fields_for @employee.department do|department_fields| %
was throwing an ActiveRecord::AssociationTypeMismatch -
Hi,
I'm following a tutorial which creates a rails project using the
scaffolding generator (in Rails 2.3.8). One of the exercises at the
end of this tutorial is to put the form used in the scaffolding's new
and update views into a partial.
So, I created a file _form.html.erb in the views folder
Cheers for the explanation, Fred.
Following your advice I passed the appropriate HTTP method to the form
in the partial and now everything works as it should.
In case it helps anyone else, I now have:
new.html.erb:
%= render :partial = form, :locals = {:url = {:action =
create}, :method = :post}
But you missed Fred's point entirely
Oops, it appears I did.
So all you need in both new.html.erb and edit.html.erb is
render :partial = 'form'
and in _form.html.erb, just
form_for @flight
Yup, that works embarrassingly well!
Thanks a lot for pointing that out.
--
Posted via
Hi,
How would I use assert select to test for the occurrence of the string
'DUS' in a 'p' tag about half way down my view?
I tried:
def test_should_display_airport_names_in_show
get :show, :id = flights(:dus_muc).id
assert_select 'p', 'DUS'
end
but Rails is just finding the first 'p' tag
Oh, right. Thanks a lot. That had been driving me mad (it's an exercise
in a book I'm reading). I changed the test method to:
def test_should_show_airport_names_in_show
get :show, :id = flights(:dus_muc).id
assert_select 'p', Departure airport:\n DUS
end
and it works perfectly.
Thanks
Just a follow up in case this helps anyone else:
You could also probably put a literal br in the error message, then
use unescape_html.
As it turns out, simple_format was wrapping everything in p tags,
which was causing invalid html to be generated in the error view of my
form.
So, I tried
Hi Luke,
Thanks very much for the reply.
You pointed me in exactly the right direction to solve my problem!!
Another approach might be to pass this value from the new action to the
create action through the form using a hidden field.
I tried this already. This had the disadvantage that the
Hi,
I have a web-based form for applicants to enter their details, created
using form_for :applicant
There is one field in the corresponding applicants table of my database
which is not represented in the form.
I want to set the value of this field in the model (it should be an
unique hash).
Radhames Brito wrote:
save it in the session hash on validation
Hi,
Thanks for the reply.
Did I understand you correctly, that you are suggesting setting a
session variable in the model (where my validation is taking place)
which I can then pass to the controller?
I didn't know that that
validate do |applicant|
errors.add_to_base(Things must check out!) unless
applicant.check_things
end
def before_save
applicant.non_represented_field = 123456789
end
before_save will only be called if your validations pass, so
non_represented_field will only be assigned to valid
pepe wrote:
If you do, please post the solution. I would like to know.
Hi Pepe,
so, after a week of trying I finally found the solution and am posting
it here in case it helps anyone else.
Error message in model:
errors.add(field, This field is too long.\nYou are using
Hi,
If I want to ensure that someone has filled out the email section of a
form I can write this in my model:
validates_presence_of :email
I can also achieve (more or less) the same thing by writing:
validate do |applicant|
applicant.validate_presence(email)
end
def validate_presence(arg)
Thanks for the reply.
Below I am assuming your arg is a field name which is
something passed by your own code.
Exactly. arg is simply the name of a field whose value I want to check.
This field is hard-coded into my program.
I don't want to execute any user generated input, rather just check
Thanks for all of the answers, you have really helped me a lot.
I was also unaware of the existence of the method '.to_sym' which will
in itself solve several of my problems. (Thanks Marnen!)
I will try out your suggested code to replace 'eval' and post back here
if I have any further questions.
Is it possible to add a line break to the middle of a custom error
message?
I have tried:
errors.add(:thesis, This field is too long. + br / + You are
using #{field.length} characters out of a permitted #{permitted}.)
errors.add(:thesis, This field is too long. + \n + You are using
pepe wrote:
Have you checked the source to see what was the HTML output?
Thanks for the answer.
The source reads:
This field is too long. l t ; br / g t ;You are using 12600
characters out of a permitted 1000.
(I have put spaces between the characters so that they don't get parsed
when I
First line%= sanitize('br /') -%Second line
I tried:
errors.add(:thesis, This field is too long. %= sanitize('br /') -%
You are using #{field.length} characters out of a permitted
#{permitted}.)
This produced:
This field is way too long. %= sanitize('br /') -% You are using
12600 characters
So, in case this helps anybody else, I managed to solve the problem by
updating to 2.3.8 in my production environment.
Turned out to be a version thing after all.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups Ruby
on
Hi,
in my development environment (Ruby 1.87, Rails 2.3.8) the following
code works fine:
%= select applicant, member_1,
option_groups_from_collection_for_select(@topics, :members, :topic, :id,
:name, @applicant.member_1), {:include_blank = true} %
Producing:
optgroup label=Clinical
Hi,
I'm trying to set a session variable in one action of
application_controller.rb, which is then accessed in a second action of
the same controller following a redirect.
Here's the code.
def cookie_test
if cookies[cookie_test].blank?
render :template = shared/cookies_required
else
My app has a 'page' model and a 'category' model.
'Page' belongs_to :category.
'Category' has_many :pages.
From the console I can create and save a page object with no problems:
p = Page.new
#Page id: nil, title: nil, body: nil, created_at: nil, updated_at: nil,
published: nil, read_counter:
Please ignore my question.
Changed: attr_accessible :category
into:
attr_accessible :category_id
and it works now.
Doh!
Checked, double checked and tripple checked before posting, but still I
missed that!
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are
In a book I'm reading the author tries to do the following from within
his controller test file:
class PagesControllerTest ActionController::TestCase
def test_create_link_in_index
get :index
assert_select a[href=?], url_for(:action = :new), :text = 'Neue
Seite'
end
end
However the
Hi,
Thanks for all of the replies.
That has really made things a lot clearer for me.
So, to summarize:
'url_for' is a method defined in ActionController::Base.
@controller is an instance variable initiated by
ActionController::TestCase containing a reference to my controller
It not that @controller contains a reference to you controller -
it is an actual instance of it.
OK, I phrased that badly.
And 'prepending a method call' sounds like you think there is some
magic going on - there isn't - methods are always called on a certain
object (omitting the object
I want to call the method 'url_for' in my functional test.
e.g. assert_select a[href=?], url_for(:action = :new), :text = 'Neue
Seite'
The book I am reading suggests doing this:
class PagesControllerTest ActionController::TestCase
def setup
def self.url_for(options,
Thanks for all of the answers.
I solved it a little differently and will explain how in case
a) it helps anybody else
b) there is a flaw in my method which I have overlooked
What I did was in the controller:
Create a model object: @applicant = Applicant.new(params[:applicant])
Wait for a post
Thanks for the replies.
I'm also not sure if doing the validation in the model would work.
Assuming that the user has a slow connection and hits submit on the form
twice, then the first time the email would be unique, so everything
would be good. However, would it not be the case that the
That's a cool idea.
I will also have a play around with that this evening.
Thank you very much.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
I have a standard form built with rails, which a user to my site can use
to submit data. Unfortunately one user just managed to submit exactly
the the same data three times in a row.
From the server logs it seems as though he didn't use his back button
and then resubmit everything. It looks more
Aldric Giacomoni wrote:
Quick hit from google after using disable form rails after click:
http://railsforum.com/viewtopic.php?id=9585
Hope this helps.
Cheers for the reply, but this is quick and dirty and doesn't solve the
problem if the user has javascript disabled.
I would rather
Thanks for the reply.
I am still confused though, as to why I cannot reproduce the error.
When I press submit ten times in quick succession then only one data set
is submitted.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this
No.
I'm at home on my computer, the application is on a web server somewhere
in Germany (as far as I know). I will however try to create the error
from a different computer, see if that works.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
It's the blind leading the blind here,
:-)
It was last night and there was relatively little going on.
I don't think traffic is the problem.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed
Lee Smith wrote:
They could have also hit the 'enter' key multiple times too, not
clicking anything.
But in either case, a small amount of javascript will prevent this.
To prevent the submit button being pressed many times I am using
%= submit_tag Submit, :onclick = this.disabled = true %
I've managed to understand / solve the problem and just wanted to post
back here in case it helps anyone else.
Having read this: http://bugs.mysql.com/bug.php?id=10035
it appears that this error is caused when a MySQL table has more than 10
columns of the type text.
I tested this by creating a
Is it possible to store data from one form in two separate mysql tables?
I need to do this because the amount of data which can be submitted via
the form, exceeds the maximum mysql row size.
If this is not possible, has anyone got any ideas how one can get round
this problem?
Thanks in advance.
Thanks for the answer Fred.
I am understanding the definition of row size correctly, aren't I?
What is happening is that I have a form (with loads of fields - some
allowing up to 4000 characters).
When a user enters an average amount of data, then the form submits and
everything is good.
When
Yeah, I found it a bit bizarre as I have reprogrammed this form in RoR.
Previously it was in PHP and could deal with data of this magnitude
without problems.
I guess I'll keep Googling and see if I come up with anything as
splitting the data into two tables seems an ugly solution.
--
Posted
I wouldn't recommend storing a single object in two tables or
splitting it across multiple rows. You'll be fighting this complexity
elsewhere in your code and it will become a real headache.
I agree there, but I'm really not sure what to do. I can literally
isolate the individual character
I wrote some tests for my controller which all worked as they should
until I added http authentication using 'before_filter :authenticate'
This is the authenticate method:
def authenticate
authenticate_or_request_with_http_basic do |user, password|
user == x password == y
end
end
Now,
I have a java script function which I want to call once the page has
loaded.
Initially it looked like this: body onload=doStuff();
I was then advised by people on this forum that it is good practice to
put this into an external js file thus:
document.observe('dom:loaded', function() {
Not really, since your ruby code doesn't know how to execute
javascript. (I often use the Test.Unit stuff from prototype to write
javascript unit tests)
Thanks for that Fred.
It's good to know that I was barking up the wrong tree.
Since reading your post I have been looking at the
Hi,
I have two select lists in my app called 'topic' and 'member'.
Based on what I select in topic, member is populated accordingly (using
java script).
eg. If I select 'subject1' in topic, member should be populated with the
values 'prof x', 'prof y' and 'prof z'.
Can I test that my java script
After a couple of days of tearing my hair out I have solved the problem
another way. I have ditched the use of the 'observe_field' helpers and
written the routine from scratch in java script.
Now it works in all of these browsers.
If anyone else stumbles accross this thread, I was greatly
Hi,
Thanks for replying
Konquerer isn't officially supported by Prototype.js which Rails uses
for it's Ajax stuff.
That's not so bad. Konqueror I can live without (although it's not
perfect)
There should be no issue with Safari 3 or 2.
I am running Safari 4 public beta on Windows and the
Hi,
I have a form with two drop down menus.
Depending on what the user selects in the first, the second should be
populated accordingly.
Here is the code I am using to do this.
View (test.html.erb):
% form_for :applicant, :url= {:action = index} do |f| %
%= f.label :topic_1 %
%= f.select
That'll teach me to re-read things before I post.
The URL is: http://igsn-application.de/test/
Sorry!
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk
Hi,
I want to call a javascript function after my page has loaded.
Currently I am using:
body onload=do_stuff()
Is there a neater / tidier way to do this using any rails specific
commands?
I found this:
document.observe('dom:loaded', function() {
$('state_field').hide();
Sorry, I forgot the script tags.
Now eveything works.
Forget I spoke :-)
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
To post to this group,
Excellent! Now take the JS out of the HTML and put it in an external
file.
Cool, that works.
Thanks a lot.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby
on
Hi,
If I have this line in my model:
validates_presence_of :name, :message = Please enter a name
and I test this with the following code:
def test_should_require_name
user= User.new(valid_user_attributes(:name = ))
assert !applicant.save
end
Is there a way to write an additional test to
Here's an article about BDD if you're interested:
http://dannorth.net/introducing-bdd
Hey, cool article. Thanks a lot.
I will read that through this evening.
I'm still a bit new to TDD and am still finding my way around, so thanks
for the advice.
--
Posted via http://www.ruby-forum.com/.
Hope you find that useful
:)
Thanks for the tip.
I'm currently reading a book Professional Webdevelopment with RoR2 by
Jens-Christian Fischer and he's just getting into Rspec.
I wasn't sure how indepth I should read that part, but now I will
definitely take the time to check it out.
--
Probably doesn't make any difference to your understanding, but I forgot
to mention that in my model is also:
attr_reader :dob_day, :dob_month, :dob_year
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are
Assuming this is a unit test then I think it should be
application.dob_day = 01 etc.
Again the attriutes are dob_day etc not date_of_birth_day.
Thanks for that Colin,
def test_should_make_date
applicant = Applicant.new(valid_applicant_attributes(:date_of_birth =
))
applicant.dob_day = 01
Hi,
Testing the view of an application with functional tests, how would one
go about checking that there are a certain number of span elements
present on a page, which all belong to a certain class.
I tried:
assert_select form do
assert_select span, :class= required_field,
Thanks for the answers
I just looked again at the html rails is generating and in fact the
element I'm looking for looks like this:
span title=required field*/span
not:
span class=required_field*/span
Would I test for this like so:
assert_select form do
assert_select form do
assert_select span[title=required field], 13
end
Excellent, that is exactly what I was after thanks very much.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to
Thanks for your reply
Looks like a case for allow_nil:
validates_numericality_of :children, :allow_nil = true
I created the field 'children' as a string (can't remember why, perhaps
because someone might enter 'none'??), so that doesn't work.
Is there any big advantage to changing the
Hi,
Is it possible to use standard validation methods in custom validation
methods?
eg:
def check_children
if children !=
validates_numericality_of :children
end
end
When I run this code I get a no method error:
undefined method `validates_numericality_of' for #Applicant:0x5d1bba4
Wow, that's very cool.
I changed the line to:
validates_numericality_of :children, :if = Proc.new { |applicant| true
if applicant.children != }
and that does exactly what I want.
Thanks for your time and help, Fred.
--
Posted via http://www.ruby-forum.com/.
you can even shorten that to
validates_numericality_of :children, :if = Proc.new { |applicant|
applicant.children != }
Cool!
Thanks a lot.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are
Hi,
I have written a method to map three 'virtual' fields in my form to one
'proper' field in my model.
It looks like this:
class Class
def map_three_fields(var1, var2, var3)
string_val = ...do stuff...
self.class_eval string_val
end
end
At the moment it is sitting at the top of my
Hi,
Thanks very much for the replies.
I will follow Michael's advice and stick it in a module.
I hope you know what you're doing.
Probably nowhere near as much as I should
What if one of the strings is
%x{echo gotcha}
or worse?
It's a date input which is mapped.
In the model the field is
Hi Michael,
Thanks for the reply.
dd-mm- (including hyphens).
Why? IMHO, the best you can do is have a look at how date_select works,
you'll notice that it already does what you're trying to do.
But isn't date_select really feeble?
For example it is very easy to make the whole
Ok, cheers for that.
I guess I will have to rethink my approach.
In defense of my current method, to make sure the date field doesn't
accept incorrect input I use ruby's 'Date.strptime' to parse the string
as a date. If it works then everything is ok, if it throws an error then
I catch this
I recommend this setup for date entry since it does help a little with
the input sanitizing: When you use a Select tag, the user input options
are known values and it allows you to be much more aggressive and
unforgiving with your input validation. Text box input requires some
fuzzy
Hi,
How can I add a class to a form generated thus:
% form_for :applicant, :url= {:action = index} do |f| %
I tried:
% form_for :applicant, :url= {:action = index}, :class = 'test' do
|f| %
but this didn't work.
Thanks for your help.
--
Posted via http://www.ruby-forum.com/.
Wonderful,that works just fine.
Thank you very much indeed.
--
Posted via http://www.ruby-forum.com/.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby
on Rails: Talk group.
To post to this group, send email
Hi,
I'm trying to produce a select menu with optgroup tags.
The HTML output should look like this:
optgroup label=Europe
option value=DenmarkDenmark/option
option value=GermanyGermany/option
option value=FranceFrance/option
/optgroup
optgroup label=North America
option
Hi Fred,
Well that worked :-)
I changed
%= f.select :member_1
into
%= select_tag :member_1
and got 100% the desired result.
Only problem is, is that I am using
% form_for :applicant, :url= {:action = index} do |f| %
to create my form and removing the reference to the applicant object
(f), means
1 - 100 of 107 matches
Mail list logo