Oddness with messages_path

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 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 Nov 26, 2011, at 2:38 PM, Cynthia K. 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?

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

Quoting Patrick J. Collins [email protected]:

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 Nov 26, 2011, at 8:03 PM, Cynthia K. 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.

Quoting Justin Ko [email protected]:

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 27 November 2011 06:15, Cynthia K. [email protected] 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
[email protected]


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users