I am working through the Rails Views chapter in the RSpec Book and
noticed something odd when I created a spec for the edit view. I am
getting the wrong value from the messages_path helper. I was kind of
wondering if it would work or not - but had expected it to fail by not
giving me an id, not by constructing the url incorrectly. With the
code below I get the follwing error message. I get the same error
when using "messages_path(message)" or "messages_path(message.id)".
1) messages/edit.html.erb renders in update form using the rails way
of doing RESTFUL urls
Failure/Error: rendered.should have_selector("form", :method =>
"post", :action => messages_path(message))
expected following output to contain a <form method='post'
action='/messages.1001'/> tag:
I can work around it by constructing the url myself with
"/messages/#{message.id}" but I am curious where the period is coming
from in the constructed url.
require 'spec_helper'
describe 'messages/edit.html.erb' do
let(:message) do
mock_model("Message", :title => "Existing title", :text => "Existing
text")
end
before(:each) do
assign(:message, message)
end
it "renders in update form using the rails way of doing RESTFUL urls"
do
render
rendered.should have_selector("form", :method => "post",
:action => messages_path(message))
rendered.should have_selector("input", :type => "hidden",
:name => "_method", :value => "put")
end
end
on 2011-11-26 22:37
on 2011-11-26 22:49
On Nov 26, 2011, at 2:38 PM, Cynthia Kiser wrote: > Failure/Error: rendered.should have_selector("form", :method => > describe 'messages/edit.html.erb' do > rendered.should have_selector("form", :method => "post", > :action => messages_path(message)) > rendered.should have_selector("input", :type => "hidden", > :name => "_method", :value => "put") > end > end What versions of rspec and rails are you using?
on 2011-11-27 00:20
> I can work around it by constructing the url myself with > "/messages/#{message.id}" but I am curious where the period is coming > from in the constructed url. It's because you should be using a singular resource name to signify that you are updating an existing record. I believe you want to do "message_path(message)" instead of "messages_path(message)" Patrick J. Collins http://collinatorstudios.com
on 2011-11-27 04:30
Quoting Patrick J. Collins <patrick@collinatorstudios.com>: > > I can work around it by constructing the url myself with > > "/messages/#{message.id}" but I am curious where the period is coming > > from in the constructed url. > > It's because you should be using a singular resource name to signify that you > are updating an existing record. I believe you want to do > "message_path(message)" instead of "messages_path(message)" D'ho! Exactly right. I am still mystified by the way in which this fails but the problem is indeed that I needed to use message_path. That works with (message) and with (message.id). And this is Rails 3.1.3 and rspec 2.7.0, rspec-core 2.7.1, and rspec-rails 2.7.0.
on 2011-11-27 04:48
On Nov 26, 2011, at 8:03 PM, Cynthia Kiser wrote: > fails but the problem is indeed that I needed to use > message_path. That works with (message) and with (message.id). Run "rake routes" and you'll see what the two "paths" generate.
on 2011-11-27 08:10
Quoting Justin Ko <jko170@gmail.com>:
> Run "rake routes" and you'll see what the two "paths" generate.
Rake routes didn't really give me much insight - but playing around in
the console did. Any argument to messages_path gets interpreted as a
format - even an integer.
> app.messages_path(:pdf)
=> "/messages.pdf"
> app.messages_path("doc")
=> "/messages.doc"
> app.messages_path(1)
=> "/messages.1"
That hadn't occured to me - even once someone pointed out my
singular/plural problem.
on 2011-11-28 16:09
On 27 November 2011 06:15, Cynthia Kiser <cnk@ugcs.caltech.edu> wrote: > => "/messages.doc" > > app.messages_path(1) > => "/messages.1" > > That hadn't occured to me - even once someone pointed out my > singular/plural problem. > Because you were running a singular route the parameter you were passing was matching the :format part of the route. So what you were doing was telling the routing that instead of html you wanted your message (which resolves to the id of the message). So instead of xxx.html you are getting xxx.1 or whatever the id is. Rake routes tells you this: Singular resource - resource :users, :only => :show users GET /users(.:format) Plural resource - resources :users, :only => :show user GET /users/:id(.:format) # you can pass an id to this route You can see that with the singular resource passing an :id to the users_path makes no sense, as the route can't understand it. HTH Andrew > -- > Cynthia N. Kiser > cnk@ugcs.caltech.edu > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > --
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.