3

currently i am trying to make my rails app single paged, but i am having some problems with the rendering of partials.. Now here is my controller code:

class WelcomeController < ApplicationController
 def create
 @welcome = WelcomeController.new
 render :partial => 'enjoy'
 end
 def index
 if params[:id] == 'enjoy'
 @partial_view = 'enjoy'
 elsif params[:id] == 'about'
 @partial_view = 'about'
 else
 @partial_view = 'main'
 end
 end
end

So this controller is linked to my view, and for the pages to change i was using the code in the index action, but now i am trying to use the thing in the create action. The code of my index.html.slim where i am trying to call the create action is:

 div id="content"
 = form_tag(:url => {:action => 'create'},
 :update => "content", :position => :bottom,
 :html => {:id => 'subject_form'}, 
 :remote => true)
 = submit_tag 'Add'

So on button click 'Add', i am expecting the content from _enjoy.slim.html, which is my partial to go into the div with id "content", but when the button is clicked and the _enjoy.html.slim is rendered it loads on top of the whole index.html.slim page... So i am stuck at that, how can i make it load into the certain div?

Thanks a ton to anyone who has an answer to this one {:

tereško
58.5k26 gold badges101 silver badges152 bronze badges
asked Mar 1, 2014 at 18:12
1
  • Which version of rails are you running? Commented Mar 1, 2014 at 18:20

2 Answers 2

4

Controller:

class WelcomeController < ApplicationController
 def create
 @welcome = WelcomeController.new
 # Remove the render
 end
 def index
 # No change
 end 
end

View:

div id="content"
= form_tag({:action => 'create'}, :id => 'subject_form', :remote => true) do
 = submit_tag 'Add'

Create welcome/create.js.erb and add the following line to it:

$('#content').html("<%= j render(:partial => 'welcome/enjoy') %>")

answered Mar 1, 2014 at 18:36
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! <3 I was just wondering, is it possible to make it run with create.js.slim file? Cause i tested it and it did not happen ;[
Maybe? Try to rename the file to create.js.slim. You'll have to change the content to: $('#content').html("#{ j render(:partial => 'welcome/enjoy') }")
Umm.. Why #{ j renderer... and not ={ j renderer.. ?
Because #{} is used for text interpolation in slim.
3

You have to add a create.js.erb file in your 'welcome' views directory and there you have to render the partial per ajax:

$("#content").html("<%= j render(:partial => 'welcome/enjoy') %>");

and in the create action of the controller you have to respond to javascript:

def create
 ...
 respond_to do |format|
 format.js
 end
end
answered Mar 1, 2014 at 18:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.