[Rails] Re: Correct syntax for prompt for select_tag
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 on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Correct syntax for prompt for select_tag
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 params[:author_id].empty? instead of just: if params.has_key?(:author_id) Does prompt pass any value in in the params? It would also be nice to understand why :prompt => true isn't working. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Correct syntax for prompt for select_tag
> Have you tried with "options"? > options = {:prompt => 'Please Select'} Just did, but didn't work :-( The effect is unfortunately the same: 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 to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Correct syntax for prompt for select_tag
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", options_from_collection_for_select(@authors, :id, :name, @selected_author), :prompt => "Please select" %> I just get: Jim ... However, adding a blank field works fine: <%= select_tag "author_id", options_from_collection_for_select(@authors, :id, :name, @selected_author), :include_blank => true %> Resulting in: Jim ... What am I missing? Ruby 1.9.2p0 Rails 3.0.5 -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] How to create new object with existing nested attributes
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 => # params = { :attachments_attributes => { "0" => {:id => Attachment.find(:last).id.to_s}} } => {:attachments_attributes=>{"0"=>{:id=>"62"}}} i.update_attributes(params) ActiveRecord::RecordNotFound: Couldn't find Attachment with ID=62 for Item with ID= I've tried using Google to find a solution, but to no avail. Would be very grateful for any advice. 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 to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Testing file uploads with Carrierwave
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 should be able to click on the "select file" button, select a file and press "submit". If the file is a PDF then it will be uploaded to the server and written to the filesystem (as opposed to a database), keeping its original name. If the file is not a PDF, then an error message will be shown and the form will be re-rendered. I would be really grateful if someone could give me some pointers as to what to test here and what not. For example, would it be sensible to test that when a file is selected and the form is submitted, that a file is actually uploaded? Or is this something which would have been tested by the developpers of Carrierwave? Thanks in advance. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Active Record Associations - best way to find all items assosciated with a specific company
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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Active Record Associations - best way to find all items assosciated with a specific company
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 to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Active Record Associations - best way to find all items assosciated with a specific company
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 I (understandably) get the error message: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: items.company_id: SELECT "items".* FROM "items" WHERE ("items".company_id = ... ) Should I write a little helper method that does what I want, something along the lines of: Item.where("items.manufacturer_id LIKE :record OR items.distributor_id LIKE :record",{:record => @company.id}) or is there a better way to do this through associations? Would be grateful for any advice. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Re: Freezing one gem in Rails 3
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 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Freezing one gem in Rails 3
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, and commit the one > gem you need. Done. This is what I'm having trouble with. I've worked quite a lot with Rails 2.x, but this is the first thing I've done with Rails 3.x and am therefore not very familiar with Bundler. I did install everything and then deleted what I didn't need, but then when I tried to start WEBrick, it wouldn't start complaining about missing dependencies. I no longer remember the exact error message as I rolled everything back after that, but I could reproduce it if that helps. What exactly do you mean by "Commit the gem you need". Am I missing a step? Thanks very much for your help so far. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Freezing one gem in Rails 3
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 are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Freezing one gem in Rails 3
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 reason) refuses to install any additional gems on his machine (I guess this is why he wrote his own paginator in the first place). My question: is it possible using rails 3.0.3, to unpack ONLY the will_paginate gem to my vendor directory, so that it will work in the project without my colleague having to install it? I tried "bundle install vendor/gems", but this installs everything to this directory. Grateful for any 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Is it possible to manually wrap a form field in a field_with_errors div after validation?
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 achieve this manually? I tried adding a nil error to the attribute: company.errors.add(:isVertrieb, nil), but this results in the field's name being output in the list of validation errors. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Problems installing exception_notification plugin
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 git://github.com/rails/exception_notification/ This works fine. However, I am trying to install the version of the plugin which is compatible with Rails 2.3 It is here: https://github.com/rails/exception_notification/tree/2-3-stable/ However, when I try the following in git-bash: ruby script/install plugin git://github.com/rails/exception_notification/tree/2-3-stable/ I get the error: fatal: remote error: rails/exception_notification/tree/2-3-stable/ is not a valid repository name. Any ideas how I can install the 2.3 branch of this plugin? Thanks in advance. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Trouble sending email image attachment with rails
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 |a| a.body = File.read("public/images/test.jpg") a.filename = "hello.jpg" end end test.jpg exists in the folder public/images and is definitely a jpg. In my previous post I had configured my app to use the mail transfer agent on my production server (presumably sendmail). To eliminate this as being the problematic link in the chain I also configured my rails app to use my gmail account to send the email. Again the mail generated in my rails app gets delivered, but with the image attachment I get exactly the same result (jpg doesn't display inline and when I download it and try to open it I get the message "File corrupt). Could anyone give me a clue as to what I am doing wrong? I'm using rails 2.3.8 Thanks very much in advance. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Trouble sending email image attachment with rails
> 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 => File.read("public/images/rails.png") but again, in the mail the image displays as broken and when I download it I cannot open it. Could I be doing anything else wrong? Thanks 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. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Trouble sending email image attachment with 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 {} attachment :content_type => "image/jpeg", :body => File.read("public/images/rails.png") end end This works, in so far as the email is delivered okay (obviously with different addresses), but the image attached to the email seems to be corrupted. It doesn't display in my mail client and when I download it and try to open it with a suitable program I get the message that the file is either too large or corrupted. I tried replacing the image with a pdf as follows: attachment :content_type => "application/pdf", :body => File.read("public/images/test.pdf") and this works fine. Can anybody give me a clue as to what I'm doing wrong? Thanks in advance. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Webrick not giving me any output
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 projects\contact>ruby script/server [2011-01-21 12:13:58] INFO WEBrick 1.3.1 [2011-01-21 12:13:58] INFO ruby 1.8.7 (2010-08-16) [i386-mingw32] [2011-01-21 12:13:58] INFO WEBrick::HTTPServer#start: pid=2108 port=3000 and, regardless of what I do in my application, that's it! When I press 'Control + C' to kill Webrick, I get: [2011-01-21 12:19:32] INFO going to shutdown ... [2011-01-21 12:19:32] INFO WEBrick::HTTPServer#start done. D:\Files\Rails projects\contact> Everything is still being written to my development log. Can anyone give me an idea as to how I could fix this situation and have Webrick telling me what it's doing again. Thanks very much in advance. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: ActiveRecord Select
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 conversion is incorrect, the rest of the article is fine though. -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Why won't write_attribute(:price, value*100) work in the console?
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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Why won't write_attribute(:price, value*100) work in the console?
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 Product.read_attribute(:price)/100.0 end def price=(value) Product.write_attribute(:price, value*100) end end In the console: r = Product.new => # r.price = 10 NoMethodError: undefined method `write_attribute' for # There is no mention of a typo in the book's errata, write_attribute doesn't appear to have been removed from rails, so I'm guessing something changed with rails which makes this not work. I did some googling and saw that shorthand for read/write_attribute() is self[] I tried the following and it worked great: class Product < ActiveRecord::Base def price self[:price]/100.0 end def price=(value) self[:price] = value*100 end end r = Product.new => # r.price = 10 => 10 Could anyone tell me why the first example produces a NoMethodError and the second one works just fine? Thanks 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. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Correct way to search datetime column by date
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 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Correct way to search datetime column by date
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 the following: @flights = Flight.find(:all, :conditions => ['arrival_airport_id = ? and departure_airport_id = ? and departure_datetime >= ? and departure_datetime < ?', @arrival_airport_id, @departure_airport_id, @date, @date+1]) This works but the code seems long and ugly. Is this an acceptable way to do what I want, or can someone point me to a better, 'prettier' method? Thanks very much in advance. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Help needed understanding "fields_for" and resultant controller code
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 - Department(#77492292) expected, got HashWithIndifferentAccess(#37082688) After a bit of googling I changed it to: <% form.fields_for :department do|department_fields| %> and everything worked fine. Thank you once again. P.S. In case it helps anyone else, this is described very well here: http://guides.rubyonrails.org/2_3_release_notes.html#nested-object-forms -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Help needed understanding "fields_for" and resultant controller code
> 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 @employee.department fails? > Nope. update_attributes simply returns true/false depending on whether > or not it succeeded. There are ways to make it raise an error, but your > code isn't doing that. Yeah, sorry, I guess I didn't include enough code with my original question. I'm (the book is) using the scaffold generator to create both 'employee' and 'department' resources. The complete update method attempts to update the attributes of @employee and @employee.department. If this doesn't work (as validation has failed, for example) it re-renders the action 'edit'. def update @employee = Employee.find(params[:id]) respond_to do |format| if @employee.update_attributes(params[:employee]) && @employee.department.update_attributes(params[:department]) format.html { redirect_to(@employee, :notice => 'Employee was successfully updated.') } format.xml { head :ok } else @departments = Department.find(:all) format.html { render :action => "edit" } format.xml { render :xml => @employee.errors, :status => :unprocessable_entity } end end end Thanks very much 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Help needed understanding "fields_for" and resultant controller 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 would be bad to update one record and not the other, but won't rails throw an error and re-render the 'edit' view if validation for either @employee or @employee.department fails? -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Help needed understanding "fields_for" and resultant controller code
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 from the form to edit an existing employee: <% fields_for( @employee.department) do |department_f| %> <%= department_f.label :name,"Department name" %> <%= department_f.text_field :name %> <% end %> The form is submitted to the update action of 'employees_controller.rb', where there is the following code: if @employee.update_attributes(params[:employee]) && @employee.department.update_attributes(params[:department]) ... employee successfully updated ... end My question is about the line "@employee.department.update_attributes(params[:department])" Is it correct to say that this code updates the attributes (in this case "name") of the Department object that is linked (via the "has_many" and "belongs_to" relationship) to the Employee object currently being edited? -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Unknown action in Rails App
> 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 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Unknown action in Rails App
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} %> edit.html.erb: <%= render :partial => "form", :locals => {:url => {:action => "update", :id => @flight}, :method => :put} %> _form.html.erb: <% form_for :flight, :url => url, :html=>{:method=> method} do |f| %> Thanks again. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Unknown action in Rails App
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 of my app. In "new.html.erb" I call the partial thus: <%= render :partial => "form", :locals => {:url => {:action => "create"} %> This works as it should and I have no problems creating a new dataset. In "edit.html.erb" I wrote this: <%= render :partial => "form", :locals => {:url => {:action => "update", :id => @flight.id}} %> but when I try to update an existing record I get the error: Unknown action No action responded to 11. Actions: create, destroy, edit, index, new, show, and update (where 11 is the id or the object I am trying to update) Using Firebug to check where the form is being sent, I see: When I run "rake routes" I see: PUT /flights/:id(.:format) {:controller=>"flights", :action=>"update"} What am I missing? Can anyone point me in the right direction? Thanks very much in advance. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Use assert_select to test for the occurence of a string
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 again. 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 to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Use assert_select to test for the occurence of a string
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 (which isn't the one I'm looking for) and returning false. 1) Failure: test_should_display_airport_names_in_show(FlightsControllerTest) [/test/functional/flights_controller_test.rb:75]: <"DUS"> expected but was <"Nr:\n RA447">. is not true. Thanks in advance. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Add line break to custom error message
Just a follow up in case this helps anyone else: > You could also probably put a literal "" 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 Maren's suggestion and came up with the following: in environment.rb require 'cgi' Error message in model: errors.add(field, "This field is too long.%3cbr %3eYou are using #{self[field].length} characters out of a permitted #{permitted}.") And in the view: <%= CGI.unescape(error_message_on :applicant, :thesis) %> This works just fine and produces valid markup in the form's error view. Thanks Maren. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Re: Problem writing to database using form_for
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 hidden field was never up-to-date as it was set during validation and thus always contained the value of the previous submission. > blah = Blah.new(params[:blah]) > > #now you have a instance of Blah. > > if blah.save > #now you have access to the non represented field > blah.non_represented_field > end This on the other hand, sorted me right out. I changed it slightly to: applicant = Applicant.new(params[:applicant]) if applicant.valid? #now I have access to the field I need do_stuff_with_non_represented_field applicant.save end What I was actually using this field for was to create a hash of all submitted values, compare it to the hash of the previous record and thus prevent double data submission. And it works wonderfully. Thanks very much 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Problem writing to database using form_for
> 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 records. Hi, Thanks for the reply. Sorry, I don't think I expressed myself very well. When I submit the form, every value the applicant has entered is validated and passed to the controller. That means that if they enter a value in the email field (for example) I can access this value in the controller in the params hash, regardless of whether validation failed or passed. As this 'non-represented' field is only getting set after a successful validation, it is not available in the params hash until everything has been filled out correctly. Before that it always shows as nil. I would like to set this value in my model with every form submission (it is a hash which is always a different value per submission) so that it is always available in the params has in the controller. I really appreciate any 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Problem writing to database using form_for
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 was possible. Any hacks around this seem to be regarded as bad practice: http://www.ruby-forum.com/topic/160769 If I have misunderstood you please let me know. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Problem writing to database using form_for
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). What is the best way to do this? Currently I am doing: validate do |applicant| applicant.check_things applicant.non_represented_field = 123456789 end This works, but the value assigned to the 'non-represented' field is only available after the applicants details have been successfully saved. If validation fails (i.e. if an obligatory field was omitted), this value is showing up in the hash of values that is posted to the controller as nil (which is bad as I always want it to be set). I would be grateful if anyone could give me an idea how to solve this. The only other thing I could think of was setting it with some kind of call back, but I couldn't get that to work. Thanks in advance. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Add line break to custom error message
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 #{self[field].length} characters out of a permitted #{permitted}.") And in the view: <%= simple_format(error_message_on :applicant, :thesis) %> Thanks very much for your help. http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Is it a security risk using eval in the model?
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. Thanks again! 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 to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Is it a security risk using eval in the model?
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 to see if the user has entered anything. I am assuming that in this case I am on the safe side using eval. Is that correct? -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Is it a security risk using eval in the model?
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) string = "errors.add(:#{arg}, \"can't be blank\") if #{arg} == \"\"" eval(string) end My question: does the method using eval pose any kind of security threat? I know the above example is silly (redefining an existing validation method), but it serves well as a simplified version of what I am trying to do, without going into unnecessary detail. Thanks in advance -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Add line break to custom error message
> First line<%= sanitize('') -%>Second line I tried: errors.add(:thesis, "This field is too long. <%= sanitize('') -%> You are using #{field.length} characters out of a permitted #{permitted}.") This produced: "This field is way too long. <%= sanitize('') -%> You are using 12600 characters out of a permitted 1000." In the source code: Publications This field is sway too long.& l t ;%= sanitize('& l t ;br /& g t ;') -%& g t ;You are using 12600 characters out of a permitted 1000. I also tried: errors.add(:thesis, "This field is too long." + <%= sanitize('') -%> + "You are using #{field.length} characters out of a permitted #{permitted}.") This produced a syntax error: /applicant.rb:505: syntax error, unexpected '<' ...s field is way too long." + <%= sanitize('') -%> + "Y... Thanks for your help so far. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Add line break to custom error message
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 submit this!) It seems that rails is sanitizing the output for me. Any idea how I can get round this? -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Add line break to custom error message
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." + "" + "You are using #{field.length} characters out of a permitted #{permitted}.") errors.add(:thesis, "This field is too long." + "\n" + "You are using #{field.length} characters out of a permitted #{permitted}.") And all other possible combinations of the above that I could think of, but nothing works. Any help greatly appreciated. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: option_groups_from_collection_for_select
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 Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] option_groups_from_collection_for_select
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: Prof ... Prof ... ... Yet in my production environment (Ruby 1.8.7, Rails 2.3.2) this code produces really weird output, sticking the previous output into an option value and escaping it:
[Rails] redirect_to kills my session variable
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 #redirect back to where the user came from end end protected def cookies_required return true unless cookies["cookie_test"].blank? session[:return_to] = request.request_uri cookies["cookie_test"] = Time.now redirect_to :action => "cookie_test" and return false end The method 'cookies_required' is called first, which sets the session variable 'session[:return_to]'. I would like to be able to access this variable in the method 'cookie_test' following the redirect. However, when I try to do so, it shows up as nil. I tried replacing the session variable with a global variable and that worked fine. I also tried sending the 'request.request_uri' variable as a parameter to the second action and that worked too. But why is my session variable dying following the redirect?? Thanks in advance -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: Can save object via console, but not in app's controller
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 subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Can save object via console, but not in app's controller
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 # >> p.title, p.body, p.category_id = "Test", "Test", "1" # >>p.save => true Yet, when I submit the same data in a post request to my controller, 'category_id' is being ignored. The page object is created in my controller thus: @page = Page.new(params[:page]) If I write the contents of 'params[:page]' to WEBrick, I see: {"title"=>"Test", "body"=>"Test", "category_id"=>"1"} If I write the contents of @page to the WEBrick, I see: # I get the same result on the console: >> params = {"title"=>"Test", "body"=>"Test", "category_id"=>"1"} >> p = Page.new(params) # >> p.save => false Does anyone have any tips they can give me? Am I missing something really obvious? Thanks in advance -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Expanding tests with own methods
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, *parameters_for_method_reference) options.merge! :only_path => true @controller.url_for(options, *parameters_for_method_reference) end end ... end Why is it necessary to define 'url_for' within the definition of the 'setup' method? Why is it necessary to define 'url_for' using self? All of my tests run just fine if I define url_for as a normal instance method: class PagesControllerTest < ActionController::TestCase def url_for(options, *parameters_for_method_reference) options.merge! :only_path => true @controller.url_for(options, *parameters_for_method_reference) end def test_create_link_in_index get :index assert_select "a[href=?]", url_for(:action => :new), :text => 'Neue Seite' end ... end Thanks in advance. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: What is @controller instance variable?
> 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 means the method is called on self) And that too :-) Thanks for clearing that up. -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: What is @controller instance variable?
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 (PagesController). PagesController inherits from ApplicationController. ApplicationController inherits from ActionController::Base. Therefore, in ActionController::TestCase, by prepending a method call with @controller, owing to the aforementioned class hierarchy, I can access methods defined in ActionController::Base. Would that be correct? Thanks again 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] What is @controller instance variable?
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 method 'url_for' is unknown within the class PagesControllerTest, as it is defined in ActionController::Base. To get around this he writes (although this is not the final solution): assert_select "a[href=?]", @controller.url_for(:action => :new, :only_path => true), :text => 'Neue Seite' How is he able to reference a method defined in another class, simply by prefixing the method call with '@controller' Could someone explain what is '@controller' in this context? I tried Googling it, but as Google ignores the '@' I couldn't get very far. Thanks in advance -- 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 rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Rails] Re: How to stop a user submitting the same data more than once.
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. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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 second time the user presses submit, the record would have been stored, but the success screen would have not been displayed (hence them feeling the need to press submit a second time). In this case the email address would no longer be unique and an error would be thrown. So, potentially the user could think that their data had not been submitted, when in fact what they would be seeing was an error caused by their second submission (their first having been successful). Nonetheless, thank you very much for the link Aldric. It is definitely useful to understand what is happening behind the scenes in Active Record, and it elaborates on the race conditions Fred mentioned. I will have a play around with validates_uniqueness_of this evening. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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 request. Look up the last email address sucessfully stored in the database. Check if it is the same as the email address currently being submitted? If so, it is possible that someone has pressed submit twice, or used the back button in their browser. This can therefore be ignored and the user redirected to the success view. If the email adresses are not identical, check if @applicant can be saved. If it can, redirect to success, if it can't re render index view. If the applicant is redirected to the success view without their data being saved (i.e. if both email addresses are the same), then they also receive a flash message that a double data entry was detected for their email address and that the record was only saved once. For this particular application it is not desired that people can apply twice and in practice it rarely happens. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
Cheers for the reply Fred. So how would one go about stopping this? Surely I cannot be the only one with this problem? -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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" %> as kindly suggested by Aldric. How would I stop users pressing enter multiple times with java script. JS is really not my forte so i would be grateful for any advice -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
I have just finished entering the same data into IE and was rather surprised to see that I could submit it many times. So, it seems to be the case that Firefox 3.5 surpresses this behaviour, whilst IE lets you submit your data as many times as you want. Do you think it would be possible to stop this by using a session. In PHP I would do something like this: session_start(); if (!session_is_registered("counted")){ $query= insert_database(); session_register("counted"); } -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
> 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 to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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/. --~--~-~--~~~---~--~~ 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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 message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to stop a user submitting the same data more than once.
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 understand what is happening when submit is pressed and work things out from there. Would be grateful for any more 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] How to stop a user submitting the same data more than once.
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 like he got a bit fed up waiting for the form to process and pressed the submit button three times. However, I didn't think this was possible in Rails. Is this correct? For the life of me I cannot reproduce this error, even by entering the exact same data the user did and pressing submit ten times in quick succession. Can anyone give me any tips on as to why this might have happened and what I can do to prevent this happening in future. Thanks in advance. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Store data from one form in two separate mysql tables?
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 form with 11 text fields. I filled ten of them with input of 15,000 chrs and submitted it - everything fine. I stuck 15,000 characters in the eleventh text field and sure enough the same error was thrown. I started deleting the characters in the eleventh text field and at some point I was able to successfully submit the form. This only appears to be an issue upwards of MySQL 4.1.11 which also explains why my old form (PHP version) didn't crash (it's operating with MySQL 4.0.24) If I understand the advice given on http://bugs.mysql.com/bug.php?id=10035 correctly then the solution to this is to save one's data in two tables, which is what I have done. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Store data from one form in two separate mysql tables?
> 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 that causes this error (seems to be around the 15,000 mark) > Are you including images or something in your object? No, not at all. It's a form used to submit an application to a uni program. Just plain text (although lots of it) As I said, i am quite confused by this as I have the new form in RoR and the old form in PHP running in parallel. The same input that causes the RoR form to crash, causes no probs at all for the PHP form. I also get the identical error on my home machine and the web server (hosting company) -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Store data from one form in two separate mysql tables?
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 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Store data from one form in two separate mysql tables?
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 the user however enters more than an average amount of data and presses submit, the following error message is thrown: ActiveRecord::StatementInvalid (Mysql::Error: Got error 139 from storage engine: ... I did a lot of Googling for "maximum size of a mysql record" and kept finding references to the max. row size (which is, as you say 65535). I took row size to mean the size of one data set (i.e. everything that gets submitted with the form). Is this correct or am I missing something obvious? Thanks very much 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Store data from one form in two separate mysql tables?
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. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Testing controller
Hi Sijo, Thanks for your reply. It certainly put me on the right track. I solved the problem slightly differently, by adding: def setup super @request.env['HTTP_AUTHORIZATION'] = 'Basic ' + Base64::encode64("user:pass") end (where 'user' is the user name and 'pass' is the password). to my test. Cheers 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Testing controller
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, for example, the following test fails and returns a 404: def test_should_get_index get :index assert_response :success end Is there any way to pass the user and password to the test. Something like: get :index, :user =>"x", :password => "y" I tried the above and all possible variations I could think of, but with no success. Thanks in advance -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] 'document.observe' and a variable generated in controller
I have a java script function which I want to call once the page has loaded. Initially it looked like this: 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() { doStuff(); }); That also worked fine. Now, I want to pass a variable to this function which is generated in my application's controller (@member) If I do this in my view, everything works fine: Yet if I do this in the js file, then nothing works (as might be expected). document.observe('dom:loaded', function() { doStuff(@member); }); Is there any way to use 'document.observe' in an external js file with a variable generated in my ruby controller? If not, what is the best practice in this case? Thanks in advance -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Testing javascript
> 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 javascript_test plugin but still unsure as to how to write a sensible test with it. I guess I'll rephrase my question and post it again. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Testing javascript
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 function is populating member with the correct values in a unit test? So far I have tried: def test_correct_value_returned applicant = Applicant.new(valid_attributes(:topic => "", :member => "",)) applicant.topic = "subject1" assert_equal ["prof x", "prof y", "prof z"], applicant.member end But I get: 1) Failure: test_test_correct_value_returned(ApplicantTest) [/test/unit/applicant_test.rb:2098]: <["prof x", "prof y", "prof z"]> expected but was <"">. Can anyone give me a pointer as to how I could go about getting this to work? Thanks 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. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Need help 'observe_field' to work in Safari or Konqueror
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 helped by this howto: http://www.trans4mind.com/personal_development/JavaScript/menuWrite.htm -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Need help 'observe_field' to work in Safari or Konqueror
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 script (http://igsn-application.de) doesn't work in this browser. It also doesn't work on Chrome 2.0.172.30. It does however, work fine in FFox, IE and Opera. What the browsers in which it doesn't work have in common is that they are built on KHTML or Webkit (a fork of KHTML). Might this have something to do with it? I tried debugging the webpage with Safari's java script debugger. I get no errors, one warning and the following output: Warning. Resource interpreted as script but transferred with MIME type text/html.(index.js) XHR finished loading: "http://igsn-application.de/test/get_members_1"; prototype.js (line 1437). It seems to me that the script is fetching the html to display in the drop down box, but not displaying it for some reason. Or maybe rendering it off of the page?? I have also updated prototype to the latest version (1.6.1_rc2) Can anyone give me a hint as to what I am doing wrong or where I could start looking to solve this problem (all Google searches so far came up blank). Thanks for your time. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Need help 'observe_field' to work in Safari or Konqueror
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" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Need help 'observe_field' to work in Safari or Konqueror
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 :topic_1, ["a", "b", "c", "d", "e", "f"], :include_blank => true %> <%= observe_field "applicant_topic_1", :update => "applicant_member_1", :with => "topic_1", :url => { :controller => "form", :action => "get_members_1" } %> <%= f.label :member_1 %> <%= f.select :member_1, ["Please select topic 1 first"] %> --- Controller: def get_members_1 @members_1 = Member.find_all_by_topic params[:topic_1] end --- View (get_members_1.html.erb): This is no. 1 (I have shortened this for sake of simplicity. Normally a lookup of all members would take place here) As you can see, when the user enters something in 'topic_1' the observe_field helper calls the method 'get_members_1' in the controller, and updates the field 'member_1' dynamically. Here is this example in action: http://localhost:3000/test My problem is that this works fine in FF, IE, Chrome and Opera, but it doesn't work at all in Konqueror. I have Googled the problem and come up empty handed. This is the java script that the helper generates: Can anybody point me in the right direction to sort this out? I am relatively ok at ruby / rails, but a beginner regarding java script / ajax. I also had a look on the rails doc page regarding observe_field, but I cannot see what I am doing wrong. I would be grateful for any 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Is there a rails equivalent of
> 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 Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Is there a rails equivalent of
Sorry, I forgot the
[Rails] Is there a rails equivalent of
Hi, I want to call a javascript function after my page has loaded. Currently I am using: 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(); $('person_country_id').observe('change', countrySelected); }); which is part of a tutorial for creating dynamic drop down menus, but when I, for example, try: document.observe('dom:loaded', function() { alert('hello'); }); this has no effect, rather it is output as html, as though I had witten document.observe('dom:loaded', function() {alert('hello');}); Grateful for any 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to test that a specific error message is thrown
> 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. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to test that a specific error message is thrown
> 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/. --~--~-~--~~~---~--~~ 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: How to test that a specific error message is thrown
Hi, That worked perfectly. 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. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] How to test that a specific error message is thrown
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 ensure that not only the name field may not be blank, but that if it is blank the message "Please neter a name" is thrown? Many thanks -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Testing fields
> 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" applicant.dob_month = "01" applicant.dob_year = "1999" assert_equal "01-01-1999", applicant.date_of_birth end works (as you might expect) absolutely fine. It's been a long day already, I think I need some more coffee. Thanks for taking the time to read through my question and find my typo. 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 to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Testing fields
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 subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Testing fields
Hi, I have a model named 'applicant'. Applicant has various fields, one of them is 'date_of_birth' I have split the field 'date of birth' into three fields: 'dob_day', 'dob_month', 'dob_year' >From the view: <% form_for :applicant, :url=> {:action => "index"} do |f| %> <%= f.label :dob, "Date of birth" %> <%= f.text_field :dob_day, :maxlength => 2 %> <%= f.text_field :dob_month, :maxlength => 2 %> <%= f.text_field :dob_year, :maxlength => 4 %> <%= error_message_on :applicant, :date_of_birth %> <% end %> The idea is that the user then types their date of birth into the text fields in the form DD-MM-. As these three fields ('dob_day', 'dob_month', 'dob_year') don't exist in the database (only 'date_of_birth') does, Rails then looks in the model, where it finds the following getter and setter methods: def dob_day=(day) @dob_day = day make_date @dob_day end def dob_month=(month) @dob_month = month make_date @dob_month end def dob_year=(year) @dob_year = year make_date @dob_year end def make_date if defined? @dob_day and @dob_day != "" and defined? @dob_month and @dob_month != "" and defined? @dob_year and @dob_year !="" self.dob = [...@dob_day, @dob_month, @dob_year].join('-') else self.dob = "" end end This has the effect that a string in the form of "01-01-2009" is returned to the field 'date_of_birth' This works great. My question: how would I write a test to check that if I set 'dob_day', 'dob_month', 'dob_year' to '01', '01', '2000', that the strig '01-01-2000' is returned? I tried this: def test_should_return_date_of_birth applicant = Applicant.new(valid_applicant_attributes(:date_of_birth => "")) date_of_birth_day = "01" date_of_birth_month = "01" date_of_birth_year = "1999" assert_equal "01-01-1999", applicant.date_of_birth end but got the error: ("01-01-1999") expected, but was (""). And this: def test_should_return_date_of_birth applicant = Applicant.new(valid_applicant_attributes(:date_of_birth => "", :date_of_birth_day => "01", :date_of_birth_month => "01", :date_of_birth_year => "1999")) assert_equal "01-01-09", applicant.date_of_birth end but got the error: unknown attribute: date_of_birth_year Sorry if this question is long winded, I lack the terminology to explain it any other way (suggestions welcome). Thanks for any help you can give. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: assert_select
> 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 the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: assert_select
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: * not: * Would I test for this like so: assert_select "form" do assert_select("span.title.required field", :count => 13) end When I run it, I get the error message: Expected at least 13 elements matching "span.required_field", found 0. I installed the recommended gem and using that the test works just fine: assert_xhtml do form :xpath! => 'count(.//span[ @title= "required field" ]) = 13' end One question however, is there any way to turn off the "...in this sample..." aspect of the output, as if the does find any errors, it returns my entire html page. E.g. 1) Failure: test_index_view(FormControllerTest) [c:/ruby/lib/ruby/gems/1.8/gems/assert2-0.5.3/lib/assert2/xhtml.rb:327:in `assert_xhtml' /test/functional/form_controller_test.rb:41:in `test_index_view' /test/functional/form_controller_test.rb:37:in `test_index_view']: Could not find this reference... ...in this sample... 300 lines of html follow -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] assert_select
Hi, Testing the view of an application with functional tests, how would one go about checking that there are a certain number of elements present on a page, which all belong to a certain class. I tried: assert_select "form" do assert_select "span", :class=> "required_field", :count => 13 end But this just found all of the span elements on the page and returned false. Grateful as ever for any 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Using standard validation methods in custom validation metho
> Well, if the column will hold numbers sure there is an advantage. You > can perform arithmetic and numeric comparisons. You can sort based on > the natural order of numbers, not based on lexicographical order. Yeah, that's true enough. I'll get it changed as I want to do everything properly. 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Using standard validation methods in custom validation metho
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 field 'children' into an interger? -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Using standard validation methods in custom validation metho
> 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 subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Using standard validation methods in custom validation metho
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Using standard validation methods in custom validation metho
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 # Is there any way around this? Thanks in advance -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Where to put additional code for model.
> 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 acceptance of values, but a Select/Options list does not - you can > validate against a fixed set of values and anything else must have come > from a Bad Guy. Hi, Thanksfor the replies. I've just been chatting with my brother-in-law who is a ruby programmer (considerably better than am I) and he pointed out that as I am calling the method which uses "eval" thus: map_three_fields :dob_day, :dob_month, :dob_year, "make_date", "dob" the method doesn't accept any user input as arguments (only three symbols and two strings) and is therefore harmless. So I guess I can stick with my original method after all. Cheers 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 to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Where to put additional code for model.
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 and output the error message that the date is invalid. >> that make no difference? > You can, of course, do your own sanitizing of input data. But in this > particular case I don't see the point. You still need to avoid anomalies > like February, 31st. I don't see any advantage in doing this stuff > yourself instead of giving ActiveRecord a chance and mop up in case it > indicates a problem. Don't get to enamored with your current approach. -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: Where to put additional code for model.
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 application crash by giving date_select false input (eg. enetering 31st February with no year. http://www.ruby-forum.com/topic/183310) > Careful! It may not be possible to enter anything exceeding the max > length in the average browser, but that doesn't stop anyone to submit > requests to your app with other tools. Good point. > %x{echo gotcha} is another way of writing `echo gotcha`. Each of these > executes the given command(s) with the right of the current user. It's > like that you wouldn't enter cd $HOME; rm -rf by yourself, so you'd > better not give someone else an opportunity to enter %x{cd $HOME, rm - > rf} for you. Even better point. > Passing arbitrary, user-provided strings to eval or class_eval *is* a > huge security hole. You're right. I did have everything working without using eval. The method with eval was just intended to make everything that bit neater. Would it then be sufficient to check the user generated input for numericality (ie. enduse that the user has only entered numbers as they are supposed to) and only carry out eval in this case, or would that make no difference? -- 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---