# Conditional RJS for :create action

**URL:** <https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023>\
**Category:** Rails\
**Created:** [August 5, 2008, 11:40am UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023 "2008-08-05T11:40:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 5, 2008, 11:40am UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/1 "2008-08-05T11:40:28Z")

</div>

I need to create Replies to Comments using RJS. The interface has  
determined two different ways of creating replies. One could be seen as  
a ‘inline reply’ approach (once posted, the reply pops in to view, in  
place of the form, and both fade away), and the other approach is when  
all existing replies are listed underneath the comment and a posted  
reply simply pops in to view without replacing the form or fading away.

I’m not good with rjs or js, so to get this working with my own hands I  
had to resort to using two different :create actions in the replies  
controller (the usual :create action was just left in place, and I  
created an :inline\_create action for the ‘inline reply’ approach,  
therefore giving me an inline\_create.js.rjs). I imagine this is bad on  
the DRY front, but it’s also caused some problems because I have two  
different types of Reply (one for a different type of Comment model - I  
cut out polymorphism because it wasn’t suitable for this app - believe  
me). Here’s where I believe the problem is;

This seems to work;  
comment.resources :replies, :collection =\> { :inline\_create =\> :post }

And this doesn’t;  
direct\_comment.resources :replies, :controller =\>  
“direct\_comment\_replies”, :collection =\> { :inline\_create =\> :post }

With the later route, I just can’t seem to post to the action. I believe  
I’ve tried everything that works with the former route, but the later  
just doesn’t want to work (I can even post to the standard create action  
in the direct\_comment\_replies controller, but as soon as I pre-fix it  
with inline\_create, I just can’t post to the custom action).

I was happy to accept the non-DRY additional :inline\_create action  
approach if it simply worked across these two Comment.replies features,  
but it appears that this custom action doesn’t work when the resource  
name (:replies) doesn’t match the controller name  
(direct\_comment\_replies). Does anyone have any ideas on how I either, a)  
create an rjs template conditional (so I can remove the :inline\_create  
actions entirely) or, b) how I can get the second route to work?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/2 "2008-08-06T21:34:13Z")

</div>

No thoughts on this one? I haven’t got any closing to fixing this yet, I  
imagine the best solution is custom js that knows how to act depending  
on what is in the view - I just don’t know js!

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/3 "2008-08-06T23:41:22Z")

</div>

Nate L. wrote:

> Neil C. wrote:
> 
> > No thoughts on this one? I haven’t got any closing to fixing this yet, I  
> > imagine the best solution is custom js that knows how to act depending  
> > on what is in the view - I just don’t know js!
> 
> Ok… not sure if i really get what you’re asking for, but the first  
> thing I noticed is that you can setup your controller to respond to  
> different types of requests. At the bottom you need to have something  
> similar to:
> 
> respond\_to do |format|  
> format.html  
> format.js  
> end
> 
> so if the request comes in as a normal post it will render your erb  
> file, but if the request is an ajax request then it will render the rjs  
> template named after your controller action.

Thanks Nate. I’m good with the differences between format.html &  
format.js (I made the app work without ajax first, now I’m sprinking  
ajax where appropriate), what I want to do is to be able to conditionals  
on the format.js template.

My interface is quite complex, so I can’t use a single rjs template to  
handle what needs to happen when a user posts a Reply from different  
areas of the interface. Having different forms and actions helps because  
I get separate rjs templates and don’t need to use conditionals in the  
RJS or js - I just post to a different URL to get a different response.  
However, this approach seems to have broken in one controller because  
the controller just doesn’t respond, and funnily enough it’s one where  
the controller name doesn’t match the resource name. I think custom js  
may be the only solution for this.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/4 "2008-08-06T22:04:50Z")

</div>

Neil C. wrote:

> No thoughts on this one? I haven’t got any closing to fixing this yet, I  
> imagine the best solution is custom js that knows how to act depending  
> on what is in the view - I just don’t know js!

Ok… not sure if i really get what you’re asking for, but the first  
thing I noticed is that you can setup your controller to respond to  
different types of requests. At the bottom you need to have something  
similar to:

respond\_to do |format|  
format.html  
format.js  
end

so if the request comes in as a normal post it will render your erb  
file, but if the request is an ajax request then it will render the rjs  
template named after your controller action.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 12, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/5 "2008-09-12T17:46:40Z")

</div>

Hi Neil,

Did you ever get anywhere with this? I’ve got the same issue, in my  
case a create action with multiple possible render options depending  
on the page. I’ve just posted a question about it and then found your  
thread.

At present I’ve just got to create actions (create and checkout\_create

- it’s for addresses which can also be created during a checkout)

Regards,

Andrew

On 6 Aug, 22:41, Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 12, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/conditional-rjs-for-create-action/145023/6 "2008-09-12T19:41:18Z")

</div>

Andrew E. wrote:

> Hi Neil,
> 
> Did you ever get anywhere with this? I’ve got the same issue, in my  
> case a create action with multiple possible render options depending  
> on the page. I’ve just posted a question about it and then found your  
> thread.
> 
> At present I’ve just got to create actions (create and checkout\_create
> 
> - it’s for addresses which can also be created during a checkout)
> 
> Regards,
> 
> Andrew
> 
> On 6 Aug, 22:41, Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I’ve kept the separate controller actions for now - it just works.  
Although, it’ll probably be cleaned-up at some point based on tips in  
these posts;

> **[Conditional RJS explained - Space Vatican](https://www.spacevatican.org/2008/5/26/conditional-rjs-explained/)**
>
> RJS is one of those nifty little things that catches your eye when you first start off using Rails. You very quickly get to add ajax and whizzy …

[http://gerhardlazu.com/blog/52/conditional-rjs](http://gerhardlazu.com/blog/52/conditional-rjs)

If you can perform RJS based on the presence of a DOM ID or class, you  
can keep things in one RJS file. However, it can get messy either way.  
If you stick with two controller actions, and you’re happy it works,  
make a ‘shared create’ method which contains the code which actually  
creates the address, that way you only have one piece of code for saving  
the records to the database. That was the only part that really got to  
me.
