Topic: RJS with RESTful routes?

I've just commented out the default routes in the /config/routes file of my app (it's a restfully-routed app).

Everything works fine with the remaining restful routes apart from the RJS.

For example, selecting from a drop-down menu with both types of routes uncommented went through e.g. these steps:

/views/adverts/_select_advertformt.rhtml

<%= select_tag :advertformat, options_for_select([... ["Rectangle", "rectangle"]... ]), /
:onChange => "new Ajax.Request(this.value, {asynchronous:true, evalScripts:true})" %>

/views/adverts/rectangle.rjs
page.replace_html("select_advertformat", :partial => "select_rectangle")

and successfully swapped out the _select_advertformat partial for the _select_rectangle partial. No problem.

But with the default routes commented out, I get a routing error:

ActionController::RoutingError (no route found to match "/adverts/rectangle" with {:method=>:post})

Not sure how to fix this. Any ideas?

Re: RJS with RESTful routes?

By default REST is restricted to the 7 actions (index, show, new, etc.). If you want to create your own actions you have to explicitly define it in your routes file. Sometimes the default route can catch these custom actions as you are experiencing, but sometimes it doesn't work. Either way, you should always define custom actions in the routes file.

That said, It's hard to tell if what you're doing here is strictly RESTful. What is the "rectangle" action exactly? Is it showing an alternative form of advert?

Railscasts - Free Ruby on Rails Screencasts

Re: RJS with RESTful routes?

Yes, you're right, it's not really restful, but I don't know how I could or if I really need to drive REST down to the level of these kinds of mundane RJS actions. And I certainly don't want to discard them. The action just swaps into the adverts/new page a drop-down list of already-uploaded advert images that is restricted to the kind of advert image required by the new advert (in this case, rectangles).

Maybe I'm confused about REST or there's a good restful reason to disallow these kinds of mundane custom actions, but I don't see the necessity of bringing REST down to this level.

But, as ever, you helped me solve the problem by telling me that custom routes are possible:

map.rectangle 'adverts/rectangle', :controller => 'adverts', :action => 'rectangle'

So, again as ever, thank you so much Ryan!

Re: RJS with RESTful routes?

I'm somewhat curious about the best way to handle this as well. Are you going to RailsConf? There's a session on Doing REST Right which I'm hoping will provide some answers. I haven't looked into it too much though.

Railscasts - Free Ruby on Rails Screencasts

Re: RJS with RESTful routes?

I guess there must be several levels of REST, as there are several forms of DB normalization, with the highest levels/forms of little more than theoretical interest (for now at least).

I'm not going to RailsConf. I'm just a beginner. Maybe next year...

Thanks again so much for your help: You taught me how to use RJS in the first place.

Re: RJS with RESTful routes?

ryanb wrote:

I'm somewhat curious about the best way to handle this as well. Are you going to RailsConf? There's a session on Doing REST Right which I'm hoping will provide some answers. I haven't looked into it too much though.

Thanks for pointing that one out, I'll definitely have to check it. I'll see you there!

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: RJS with RESTful routes?

ryanb wrote:

I'm somewhat curious about the best way to handle this as well... I haven't looked into it too much though.

Old thread, but I just read something in Apress's "Practical Rails Social Networking Sites" that made sense to me on this issue (p.57): "Since you are not creating a REST resource but simply providing actions for the web site, these do not have to be constrained to the REST actions."

On another note, the author then suggests that "[t]hese actions can be called using the URL format /<controller>/<action>/<id>." Following the PeepCode screencast on REST, I thought that this standard URL format should be disallowed in RESTful apps by deleting the respective code from the bottom of the routes file. I've been defining e.g. all the routes for my RJS actions individually.

With Rails 2.0 on the horizon, does anyone know what the emerging conventions are regarding REST and RJS actions or (more generally) regarding non-REST-resource routing?