respond js different controller

8 views
Skip to first unread message

fugee ohu

unread,
Sep 8, 2018, 10:58:47 AM9/8/18
to Ruby on Rails: Talk
I wanna modify the dom on the coversations show page which contains a new message form for the messages controller Presently the create action in the messages controlller redirects to conversations/show I wanna respond js with simply render conversations/show Can I do that?

Walter Lee Davis

unread,
Sep 8, 2018, 12:50:33 PM9/8/18
to rubyonra...@googlegroups.com
Yes, you can. If you have the respond_to block in the controller, then anything you put inside the block can do whatever it wants to. Here's one from a controller I made some time ago with the scaffold generator:

def update
respond_to do |format|
if @country.update(country_params)
format.html { redirect_to @country, notice: 'Country was successfully updated.' }
format.json { render :show, status: :ok, location: @country }
else
format.html { render :edit }
format.json { render json: @country.errors, status: :unprocessable_entity }
end
end
end

Only one block will respond to the action here, depending on the format and the result of #update. So this won't violate the single render/redirect constraint on controller method.

At the Ruby level, this is just a block inside a block.

Walter

> On Sep 8, 2018, at 10:58 AM, fugee ohu <fuge...@gmail.com> wrote:
>
> I wanna modify the dom on the coversations show page which contains a new message form for the messages controller Presently the create action in the messages controlller redirects to conversations/show I wanna respond js with simply render conversations/show Can I do that?
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/efcc9162-a8f3-4d9c-98ff-690dfc04530f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Walter Lee Davis

unread,
Sep 8, 2018, 2:29:21 PM9/8/18
to rubyonra...@googlegroups.com
> On Sep 8, 2018, at 12:58 PM, fugee ohu <fuge...@gmail.com> wrote:
> I think respond_to within the action and respond js are different but a separate subject I put respond js at the top of the controller and created a create.js.erb view At the end of the create action there's nothing in substitution of repsond_to do |format| I'm just expecting it to render create.js.erb (in the messages controller still) and then the content of create.js.erb is gonna be $("#conversation_area").html("<%=j render('conversations/show') %>")
> Does that make sense?
>

Try using the long-hand, not the shortcut of using respond js at the top. The long-hand is what the shortcut becomes eventually, so it's clearer why it works that way. Yes, you will have to put a separate format.js in each method, but when you do that, you can see exactly what it is doing, and make each method respond the way you want it to.

Walter


>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6ba94744-42bd-4839-b3f6-6df1ec7e8ccc%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages