This is my view: (show.html.erb)
<%=link_to vote_for, vote_for_question_path(#question), :remote => true%>
This is the action that the link calls in the question_controller:
def vote_for
#quest_vote_for = Question.find(params[:id])
current_user.vote_exclusively_for(#quest_vote_for)
#total_vote = current_user.total_votes
respond_to do |format|
format.js{}
format.html
end
end
What I want, is to update the content of the show.html.erb after the link is clicked. (I am using ajax (remote=>true)). The show.html.page should update itself with the new value of #total_vote.
How can I do this?
Add vote_for.js.erb, then add the following content to your file:
$("body").prepend("<h1><%= #total_vote %></h1>")
Related
in my app I have show action with show.html.erb page, where I show the recipe and the comment for this recipe. and I wand add the ajax functionality to comment adding. in controller RecipeController I have add_new_comment action. can I from add_new_action redirect to show action from format.js?
show action:
def show
#category = Category.where(id: #recipe.category_id).take
#comments = #recipe.comments.recent.all.paginate(page: params[:page], per_page:15)
end
add_new_comment action:
def add_new_comment
#recipe = Recipe.find(params[:recipe_id])
if params[:comment].present?
#comment = #recipe.comments.build
#comment.comment = params[:comment]
#comment.user = current_user
respond_to do |format|
if #comment.save
format.html {redirect_to recipe_path(#recipe)}
format.js {render action: 'show', :id => #recipe.id}
end
end
end
end
and I have add_new_comment.js.erb
$('#comments').html("<%= j (render 'comments') %>");
You can use window.location.replace, or window.location.href as described here
Just add the line to your add_new_comment.js.erb and it should work.
$('#comments').html("<%= j (render 'comments') %>");
window.location.replace("<%= recipe_path(#recipe) %>");
Please try this code.
window.location.href = "<%= recipe_path(#recipe) %>"
So if I understood well, you want to redirect to the recipe show page after adding the comment so you can update the page with the new comment? If so you can do that without redirecting, you can just append the newly created comment to the recipe comments.
add_new_comment.js.erb
$('#comments').append("<%= j (render #comment) %>");
If you really want to redirect use:
window.location.assign('your_url');
Try rendering JS with:
render :js => "window.location = '/recipes/#{#recipe.id}'"
Hope it helps :)
I recently started with Rails 3.2 and am stuck while trying to implement some ajax functionality in my app. I followed this railscast completely (http://railscasts.com/episodes/240-search-sort-paginate-with-ajax). Beyond this I want to implement a shortlist button for each product which lets user shortlist products and store them in the session. I also want a small list of shortlisted products to show up on the same page, which needs to be ajax updated.
I am wondering what is the best way to do that. I currently implemented the link_to buttons with remote tag and a helper function to change the link to shortlist/unshortlist. I also, used a conditional div to show the shortlist based on the length of shortlist. However, the issue is that whenever I shortlist, the order of the products table is also reset.
Here are snippets of my code :-
Application_helper.rb
def shortlist_unshortlist_link(product_id )
if (user_session.already_shortlisted? product_id )
link_to 'Unshortlist', { action: 'unshortlist', id: product_id }, remote => 'true'
else
link_to 'Shortlist', { action: 'shortlist', id: product_id }, remote => 'true'
end
end
def shortlist_div
shortlist=user_session.shortlist
if (user_session.shortlist.length > 0)
render :partial => 'pages/shortlist_fields'
end
end
products/index.html.erb
<div id="product">
<% #products.each do |product| %>
<tr>....
<td><%= shortlist_unshortlist_link(product.id.to_s) %></td>
</table>
</div>
<div class="shortlist"><%= shortlist_div() %> </div>
products_controller.rb
def shortlist
user_session.add_to_shortlist(params[:id])
redirect_to products_path
end
def unshortlist
user_session.remove_from_shortlist(params[:id])
redirect_to products_path
end
I think, the issue is because of redirect_to, but I am not getting how to avoid this without hurting the functionality. Am I on a totally wrong path here. Is this a good way to implement this. Any suggestions.
thanks,
Amit
you should use in shortlist method,
respond_to do |format|
format.html {redirect_to products_path }#shortlist.html.erb
format.js #shortlist.js.erb
end
and write your java script to #shortlist.js.erb file.
Do the same with unshortlist.
I agree with Sush, you didn't response to the ajax request from link_to.
In your controller, shortlist method, response to the ajax request
respond_to do |format|
format.html {redirect_to products_path }
format.js
end
By convention in Rails, format.js will execute the js.erb file with the same name as your method.
And in the shortlist.js.erb, you may write something like:
$('#shortlist').html('<%= escape_javascript(render "pages/shortlist_fields")%>');
Besides, you can also call the same js.erb file.
In the unshortlist method, you can do it like that:
respond_to do |format|
format.html {redirect_to products_path }
format.js {render :action=>'shortlist'}
end
All I want to do is update a text field on my page with a value when my Rails remote form is submitted.
All the source code be found at github here https://github.com/iamliamnorton/fasdac
The form is on the index page...
Controller code...
def index
#calculator = Calculator.new(params[:calculator])
if #calculator.valid?
respond_to do |format|
format.html { redirect_to calculator_path(:calculator => params[:calculator]), :notice => 'Calculation was successfully completed.' }
format.js
end
else
respond_to do |format|
format.html { redirect_to calculator_path(:calculator => params[:calculator]), :alert => "Calculation was not successfully completed. #{#calculator.errors.full_messages.to_sentence}" }
format.js
end
end
My input form
<%= form_for(#calculator, :url => calculator_path, :remote => true) do |f| %>
...
<% end %>
And the index.js.erb
$("#result").value = "SUCCESS!";
I'm trying to update a input text field with name and id 'result'. I cannot get this working, I've tried many different variants in the index.js.erb but cannot get it working. What am I doing wrong?
It looks like the data is being sent to the controller as I can see in the server console
...
Processing by CalculatorController#index as JS
...
But I can't get the text area to update
index.js.erb
$('#result').val("Success!");
Since, I am new to rails, so I have want to know a small functionality.
I have a reports model in my rails 3 application(not by scaffolding). I am displaying reports one by one through ajax functionality. I want to add a delete link to my each report. I have also created the destroy method in my controller. Now, I don't know how to delete a specific report when I click on the delete link of that particular report.
Here's my controller code:-
class ReportsController < ApplicationController
def index
#reports = Report.all(:order => "created_at DESC")
respond_to do |format|
format.html
end
end
def create
#report = Report.create(:description => params[:description])
respond_to do |format|
if #report.save
format.html { redirect_to reports_path }
format.js
else
flash[:notice] = "Report failed to save."
format.html { redirect_to reports_path }
end
end
end
def destroy
#report = Report.find(params[:id])
if #report.destroy
format.html { redirect_to reports_path }
format.js
end
end
end
You can assume that my reports are being displayed in the twitter-timeline format and I want to add the delete report feature to each report. Please help me out.
In your view you'd add a link, button, etc. to send the delete action back to the server.
Using link_to for example:
link_to("Destroy", report_path(report), :method => :delete, :confirm => "Are you sure?")
You can do the same with button_to.
Update:
Sorry I missed the AJAX mention (thanks Jeffrey W.).
You'll also want to add :remote => true if you want to send the delete via AJAX.
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