Ruby on rails validates with code not working - ruby-on-rails-3

Is there a mistake here? I want the entry to start with http://amzn.com/w/ followed by anything. But what ever I enter I get the error message.
validates :wl_url, format: { with: /\A(http:\/\/amzn\.com\/w\/)+([a-z0-9]*)\z/,
message: 'Please make sure the Wish List code starts with http://amzn.com/w/, if unsure see "how it works" page for instructions.'}

Try this syntax:
validates_format_of :wl_url, :with => /\A(http:\/\/amzn\.com\/w\/)+([a-z0-9]*)\z/i, :message => "Please make sure the Wish List code starts with http://amzn.com/w/, if unsure see "how it works" page for instructions."

Related

Where can I find the cause of this error message? (rails 3.2.8 nested form)

My New Contract Form use to work ... it still needs some refinement ... but the current version use to work, but now I am getting this message:
3 errors prohibited this codeline from being saved:
There were problems with the following fields:
Contract can't be blank
Client can't be blank
Code can't be blank
I do not have any fields named Contract, Client, or Code ... those are my models.
Here is the params which I raised in the codelines_controller:
{"contract_attributes"=>{"authnum"=>"900700", "st_date"=>"2012-09-03",
"end_date"=>"2012-12-31"}, "client_attributes"=>{"f_name"=>"Esme", "mi"=>"J",
"l_name"=>"Inneed", "birth_date"=>"1986-03-18", "address1"=>"62 Southy View",
"address2"=>"", "city"=>"Fromm", "zip_code"=>"55803", "state"=>"WI",
"medicare_num"=>"3008769788", "medicaid_num"=>"765894567", "member_num"=>"6709875-3",
"soc_sec_care_mgr"=>"Caring Manager", "sscm_ph"=>"1-444-444-4444",
"nurse_care_mgr"=>"Caring Nurse", "ncm_ph"=>"1-555-555-5555", "emer_contact"=>"Always
Here", "ec_ph"=>"1-666-666-6666", "pri_care_phy"=>"The One",
"pcp_ph"=>"1-777-777-7777"}, "code_attributes"=>{"code_name"=>"S-5463",
"status"=>"Active", "description"=>"Transition from schl to work"},
"units_alloc"=>"100.00"}
as you can see all of the fields that need data have data and are not 'blank'
I did contract out to see if I could get a professional to help me design a button to add extra attributes to the form, but I had to cancel the contract because they were not communicating. So part of this process required that I make them collaborators to github and heroku. Maybe they had to change something to work on the project?
Can someone give me some guidance on how to resolve this issue?
Thanks.
look in app/models/theModelThatIsUsed.rb and if you find something like:
validates :contract, :presence => true
validates :client, :presence => true
validates :code, :presence => true
delete that lines

Ruby on Rails: How to customize validation error message?

I have a following code:
validates :name, :presence => true
Error message produced is "Name can't be blank"
Instead of using the actual attribute name (in this case "name") I want to
display message as "Registration name can't be blank".
How do I overwrite the default message on the validations? I tried appending :message
but it didn't work...
Thanks!
In en.yml file define custom keys as:
activerecord:
attributes:
model_name:
attribute_name1: key1
attribute_name2: key2
......
This key will be used automatically when errors are generated.
Reference: http://edgeguides.rubyonrails.org/i18n.html#translations-for-active-record-models
(5.1 Translations for Active Record Models)
This will do the trick:
validates :name, presence: { message: "Registration name can't be blank" }
or the old hash rocket syntax version:
validates :name, :presence => { :message => "Registration name can't be blank" }
Its a little late now (after about 35 days) to answer this. So, sorry for this. But just wanted to share that I had used a gem, more than a few months back, for custom error messages.
This plugin allows you to omit the attribute name for specific messages. All you have to do is begin the message with a ‘^’ character.
I just checked it at
https://github.com/nwise/custom_error_message
& it has not been updated since march. So, i probably used it at the right time.
ps : Your answer for defining the custom keys in the yml file is more appropriate though.

RAILS3: Set query parameters to post in functional tests?

I have a Rails3 app that has Workflows, and they have many WorkAssignments. WorkAssignments have a field, work_sent_date, the date work was sent to the person. On the Workflow edit screen I display a work sent date field, but Workflow does not have an attribute work_sent_date. If any work assignments have a date, I display the most recent one and it can't be edited. If all are blank, I display a text box that is editable and in WorkflowController#update, if that date is filled it, the work assignments' work_sent_date field get that date.
It works when I test it manually. I suppose I could just create an attribute for it, but I'm trying to be slick and not have redundant data.
I'm trying to write a test for this. First I assert that the WorkAssignment#work_sent_date is blank for all work assignments. Then I try a "post :update" and I can't figure out how to pass in the work_sent_date value, which is a form field but not an attribute. What I want to do is something like.
test "setting work_sent_date in wf sets it in wa" do
#workflow.work_assignments.each do |wa|
assert wa.work_sent_date.blank?
end
get :edit, :id => #workflow.id
assert_response :success
post :update, :workflow => #workflow.attributes, :parameters => {'work_sent_date' => Date.today.to_s}
#workflow.work_assignments.each do |wa|
assert_equal(wa.work_sent_date, Date.today)
end
end
But that parameters field doesn't work. There's no error, but I keep getting failures because wa.work_sent_date is still nil, so it's not getting passed in correctly. How do I pass today's date in as an extra parameter?
(Or maybe there's a better way to do the whole thing, which I would gladly consider.)
I know this is complicated. I hope I explained it well. Any suggestions would be appreciated. I've googled to death and can't find anything.
Found my problem. Syntax way wrong. Here's what it should be. This works.
put :update, :id => #workflow.id, :workflow => #workflow.attributes, :work_sent_date => Date.today.to_s
You can also refactor out the create and edit as follows:
protected
def edit_workflow(workflow, options = {})
post :update, :id => workflow.id, :workflow => workflow.attributes.merge(options)
end
def create_workflow(options = {})
post :create, :workflow => {}.merge(options)
end
end

Rspec 2 + Devise + Cancan - Factorygirl = Controller Testing not working

I'm trying to move to rspec for testing, but I can't get controller testing to work with user authentication. Testing a route that doesn't require a user works.
require 'spec_helper'
describe UddsController do
include Devise::TestHelpers
render_views
before (:each) do
#user = User.new(:email => "test#user.com", :username => "test123")
#user.roles << Role.find_or_create_by_name("admin")
#user.save
sign_in #user
end
it "should get index" do
get :index
response.should be_success
end
I just get
2) UddsController should get index
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/udds_controller_spec.rb:21
I'm not using factory_girl. All the example fixes here and on google seem to. I don't see why what I've done wouldn't work. Error is useless in debugging.
I know this question has been asked to death. I've read every blog and forum I can find. I'm at a dead end.
It might be redirecting. be_success returns false for any non-2xx status codes (like 302). Try checking the value of response.status -- if it's redirecting, it probably means your authentication or authorization scheme is producing an unexpected result.
Try adding puts response.body before your assertion, and see what the page you are requesting says. If you can't figure it out from there, perhaps edit your question to include the output?
If you are using devise's "confirmable" module which makes users click a link in an email to confirm their address, then when you use "sign_in" in the tests you also have to fake email confirming. It looks something like this:
sign_in #user
#user.confim!

How to get rid of devise current password requirement when editing a user?

I've followed the instructions here:
http://github.com/plataformatec/devise/wiki/How-to-edit-user-form-without-current-password
But it seems to ignore that and still validate and require current password.
In fact, I can do whatever I want to app/controllers/registrations_controller.rb including putting tonnes of syntax errors in, and it still asks for the current password to be present.
What am I missing?
Hard to quess, as you pasted no code, but it looks like you need to point devise to a different controller:
devise_for :user, :controllers => { :sessions => "..." }