I have rails application where I have part of views as partials and I re-factored couple of views as SPA. I have problem with one functionality. In one place I was rendering partial without calling controller like following:
def create
school_service.update_preview
#user = get_user
#result = prepare_result
render 'school/result/show', locals: {
pupil: #user,
result: #result
}
end
I was calling this method using form. Now I call this from JS using AJAX. Is it possible to render that view in the same way? I wouldn't like to rewrite 'school/result/show' to SPA. Thanks for all answers.
Your question says I was calling this method using form. Now I call this using AJAX. Is it possible to render that view in the same way?
Since you are using AJAX i assume you'll have something remote: true in your form something like:
<%= form_for(#user, remote: true) do |f| %>
# your fields
<% end %>
This will take you to the create action in your controller and you can have a respond_to block in your controller to handle your js format
def create
#user = get_user
#result = prepare_result
respond_to do |format|
format.js {}
end
end
This will allow you to have create.js.erb file in your views where you can write your js to render your partial
$(".your_parent_element_class").html("<%=j render partial: "school/result/show",locals:{pupil: #user, result: #result} %>")
For more details checkout working with javascript in rails
Related
Simple Problem:
I have a button and whenever the user will click on this button, I want to redirect him to another action in another controller. How could I do that?!
This code would be in the .html.erb file
<%= form.submit "Create Request"%>
Note: i don't want to use link_to! Additionally, I could not use redirect_to in the controller because i used render there.
In your create action within the controller, you can redirect wherever you want after save the values.
For example:
def create
#model = Model.new(permited_params)
respond_to do |format|
If #model.save
format.html { # todo block with redirect }
end
end
end
Using remote: true in the form , Ex:
<% form_tag create_path(params_you_needed), remote: true do |form| %>
add rails js for create method create.js.erb
$('#id').html('<%= j render(:partial => "file/location", :locals => {data}) %>');
and in that you can render the file you need!
I have my jquery ajax success as
success: function(data) {
$('#someId').html(data);
}
I have a partial file in the name of _information.html.erb
How do i render my ajax success response to rails partial view(information).
Most of the resources showing something like this
$('#holderDiv').empty().append('<ul> <%= j render #comments %> </li>')
But i didn't feel comfortable with it. Any other way to solve it.
UPDATE
Here's some more info in response to your comments.
First please read this Rails Guide on Javascript for more info.
update.js.erb is your view. Instead of having an update.html.erb file for your view, the respond_to block with format.js in your controller will send update.js.erb (formatted as javascript code) back to your jquery function.
update.js.erb could contain pure javascript. However it is processed by the server before being converted to javascript, so you can embed any ruby code you want. That ruby code gets converted into javascript.
If you use chrome developer tools, you can look in the "network" tab after your jquery call runs. You'll see a new entry appear for the AJAX call you just made. If you click on the entry, you'll see the javascript that was returned.
I've updated the update.js.erb file below slightly to show how you can put regular javascript code in the .js.erb file. The first line is javascript. The second line is ruby code which the server converts into javascript. So by the time that it gets to your browser, the entire update.js.erb file has been converted into javascript.
Hope that helps...
Original Answer Below:
Option 1:
Assuming that your jQuery success function is tied to the successful completion of a controller action (I'll use the edit action for my example), you would create a view called update.js.erb which will be called after a successful edit.
Controller:
if #user.update_attributes(params[:user])
respond_to do |format|
format.html { redirect_to #user, notice: "Successfully updated user." }
format.js
end
else
# ...
end
Because this is being called from javascript and you have format.js in the respond_to block, update.js.erb will automatically be called.
update.js.erb:
console.log('see... this is a regular javascript call.');
<%= render partial: 'information', format: 'js' %>
Option 2
The snippet you included:
$('#holderDiv').empty().append('<ul> <%= j render #comments %> </li>')
will only work in a js.erb file, where embedded ruby code is first processed then converted into javascript code. That would work in a situation such as:
Controller:
def create
user = User.new(params[:user])
respond_to do |format|
if #user.save
#comments = 'some comments to display!'
format.js
else
# ...
end
end
end
create.js.erb:
$('#holderDiv').empty().append('<%= j render #comments %>')
When app gets javascript request it renders application.js.erb and index.js.erb by default, but i want to use index.html.erb and application.js.erb. How can i achieve that?
I want to use :
Application.js.erb + index.html.erb instead of
Application.js.erb + index.js.erb
add .html to end of partial name you want to render.
Example:
<%= render :partial=>'some_partial.html' %>
Instead of:
<%= render :partial=>'some_partial' %>
In your controller code you could
def index
#get records and whatnot
respond_to do |format|
format.html # index.html.erb
format.js { render :action=>:application }
end
end
Note: I don't know why you are doing this, but this will work. I recommend working within the rails standards.
I have a an application layout template with:
<%= yield(:railed) %>
To handle contact in the right rail (right column 300px). In the actualy DEF SHOW page I use:
<%- content_for(:railed) do -%>
HTML STUFF goes here
<%- end -%>
The issue I'm having now is that for one of my controllers, Im using AJAX to hit DEF Show, and inject the content into the page. This work fine expect for it doesn't get the railed content as the layout template isn't being used in the format.js response.
So what's a smart way I can get the railed contet display with the AJAX request, and not have to write to separate pages for AJAX & Non-AJAX (direct URL).
Thoughts? I know some Rails genius has figured this out already :)
My controller:
def show
#thing = Thing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.js
end
end
show.js.erb:
<%= render :partial =>"thing/show" %>
It's a bit of a hack but this is how I did it in rails 3.1 with haml and jquery:
show.html.haml:
- if request.xhr?
#content_for_sidebar.hidden
=#view_flow.get(:sidebar)
ajax_callbacks.js:
$("#nav a[data-remote]").live('ajax:complete', function(status, xhr) {
$($(this).attr('rel')).html(xhr.responseText);
$('#sidebar').html($("#content_for_sidebar").html());
$("#content_for_sidebar").remove();
});
One of my model objects has a 'text' column that contains the full HTML of a web page.
I'd like to write a controller action that simply returns this HTML directly from the controller rather than passing it through the .erb templates like the rest of the actions on the controller.
My first thought was to pull this action into a new controller and make a custom .erb template with an empty layout, and just <%= modelObject.htmlContent %> in the template - but I wondered if there were a better way to do this in Rails.
In your controller respond_to block, you can use:
render :text => #model_object.html_content
or:
render :inline => "<%= #model_object.html_content %>"
So, something like:
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :text => #model_object.html_content }
end
end
In latest Rails (4.1.x), at least, this is much simpler than the accepted answer:
def show
render html: '<div>html goes here</div>'.html_safe
end
Its works for me
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :inline => "<%== #model_object['html'] %>" }
end
end