How to work with ajax when showing different partials - Rails 3 - ruby-on-rails

So I was wondering how to work with the link_to method and ajax in Rails 3, when redering different partials.
Example:
Let say I have two link in show.html.erb and every link have a partial to render.
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true} %></li>
And we are going to render the partials in this div:
<div id="profile-data">
...render here...
</div>
In the UsersController we have our call methods for each partial:
def group1
respond_to do |format|
format.js
end
end
def group2
respond_to do |format|
format.js
end
end
And of course we have our js files in the view user folder:
group1.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group1')) %>");
group2.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group2')) %>");
So my question is:
is this the right way to render different partials with ajax? Are I missing something? Do have to route them some way?
This code dosent work right now and I dont know why, any help would be appreciated.

You need to explicitly state that you want to make a javascript request in your link_to.
This can be done be setting the format in the options hash to js with: :format => :js.
So in your case it should look like this:
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true, :format => :js} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true, :format => :js} %></li>

The link_to should be somewhat like
<%= link_to "Group1", {group1_users_path, :format => :js} , :method => :get, :remote => true %>
or
<%= link_to "Group1", {:controller=>:users,:action=>:group1, :format => :js} , :method => :get, :remote => true %>
or if it is a member route and needs user_id
<%= link_to "Group1", {group1_users_path(#user) :format => :js} , :method => :get, :remote => true %>
The second param for link_to is the url options so only the url related option go in it, others should be out of the hash or else they will be just passed as params.
Check out more details at rails cocs they have some neat docs and examples
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You need to have group1 and group2 in routes file
it should be like
resourses :users do
collection do
get "group1"
get "group2"
end
end
this will add helpers group1_user_path and group2_user_path
I recemmond you to go through rails docs thoroughly
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Related

Rails 3.2.x remote=>true still reloads a page

I've been searching all over and can't figure out why this isn't working.
I'm trying to test a very basic ajax action. Here are my code:
Controller:
def commit
respond_to do |format|
format.html { redirect_to :action => "index" } # see note 1
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
end
end
View:
<%= link_to "commit", :action => "commit", :remote => true %>
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
<%= submit_tag "commit" %>
<% end %>
<div id='message'></div>
commit.js.erb
console.log('committed');
$('#message').html("committed");
The problem is I'd get to the commit method, but the page would reload, which defeats the point of remote=>true
Also the commit.js never seemed to get called.
Note 1: If I exclude this line, I get the blank page to /commit. Including it makes the page just reload
Note 2: I've tried both of these approaches suggested by other SO posts
Note 3: I've tried both using link_to and form_tag
Can anyone help? Thanks!
Why did you put there 2 lines?
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
Remove the second one!
Replace:
<%= link_to "commit", :action => "commit", :remote => true %>
with:
<%= link_to "commit", commit_path, :remote => true %>
The same with the form:
Make your:
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
as:
<%= form_tag(commit_path, :remote => true) do %>
Note: POST is the default behavior, you can omit it from form_tag.

Rails Ajax Render Partial yields 500 Internal Server Error and Java::JavaLang::NullPointerException

This symptom has been reported in various contexts, most of which I've already searched yet found no clear answer. Here is my context.
In full view index.html.erb:
<div id="add_thingy" style="display:none;">
<%= form_tag (
:action => 'create',
:remote => true,
:update => "thingy_list",
:position => :bottom,
:html => {:id => 'thingy_form'}
) do %>
ipv4_address: <%= text_field "thingy", "ipv4_address" %>
<%= submit_tag 'Add' %>
<% end %>
</div>
In partial view _index.html.erb:
<li><b>
<%= link_to thingy.ipv4_address, "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'page1', "http://"+thingy.ipv4_address+"/page1.html", :target => '_blank' %>
<%= link_to 'page2', "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'Edit', {:action => "edit", :id => thingy.id} %>
<%= link_to 'Delete', {:action => "delete", :id => thingy.id},
:confirm => "Delete thingy at #thingy.ipv4_address?", :method => :delete %>
</b></li>
In thingy_controller.rb (other things I've tried are shown commented out):
class ThingyController < ApplicationController
layout 'standard'
def index
...
end
...
def create
#thingy = Thingy.new(params[:thingy])
if #thingy.save
# redirect_to :action => 'index'
# $(“#index”).rhtml(“<%= escape_javascript render( #thingy ) %>”);
# render :partial => 'index', :object => #thingy_list
# render :partial => 'index'
render :partial => 'index', :object => #thingy
end
end
...
end
All other aspects of my implementation test clean, including the first commented line above. It's when I try any of the subsequent 4 lines that I get the indicated errors.
I'll use the first commented line for now, though it defeats the main purpose of using Ajax.
Speculation abounds on this question, so I'm looking for information on how it has been fixed in a similar context rather than suggestions on how it might be fixed.
On behalf of myself and the community, thanks for your help!

How do I create a link that passes an ID for a custom action in Rails?0 <%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>

I have the following code:
<%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>
'showpdf' is an action in my Letters controller.
My expectation is that this link should yield the following:
http://domain.com/letters/showpdf/id.pdf
But instead, I get:
http://domain.com/letters/showpdf?id.pdf
If the default routes are :controller/:action/:id shouldn't this work?
Do I need to do something in the routes, even though the format for default appear right? Thanks!
Have you tried something like:
<%= link_to "PDF", :action => "show", :id => letter.id, :format => :pdf %>
where your route would be
:controller/:action/:id.:format
and in your controllers "show" action:
respond_to do |format|
format.pdf .....
format.html .....
end

button_to :action => 'destroy' looks for 'show'

This seems incredibly similar to a question I had answered just a few days ago, but the solution then isn't working now.
I'm building a rails app, and I am trying to have a button_to trigger a destroy in a different controller.
the code I have for the button is
<%= button_to "delete", :controller => :meals,
:action => 'destroy',
:recipe_id => recipe.id,
:method => :post >
when I click the delete button, i get a
'no matches for meals/3' which is the current meal_id.
the destroy in the meals controller looks like this
def destroy
#meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
#meal.destroy
respond_to do |format|
format.html { redirect_to :controller => "user" , :action => "show" }
format.xml { head :ok }
end
end
it appears as though the button_to is completely ignoring the :action and requesting show which does not exist and shouldn't exist.
And how you part of routes.rb for that one looks like?
Because if you use map.resources then destroy has same path as show but :method => :delete(which is virtual verb implemented by form and _method=delete param).
Try this:
<%= button_to "delete", {:controller => :meals,
:action => 'destroy', :id => recipe.id }, :method => :delete %>
or if recipe is instance of Meal class then
<%= button_to "delete", #recipe, :method => :delete %>
Mind the curly brackets.
I know it is way too late for an answer but hope it may help somebody(using Rails 4).
<%= button_to "delete", meal_path(:id => recipe.id), :method => :delete %>

Highlighting the link_to_remote link

Hi i have the rails application. when i call the home controller i have index action . in the index.html.erb .i have some link_to_remote links.
<li><%=link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %></li>
<li><%=link_to_remote "Test",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html => { :id =>'cb1'},
:with => "'choose=' +encodeURIComponent('value')" %></li>
After clicking the "Example" and "Test" option the respective div got updated .... i want to highlighting the options after user click happened... consider If "Example" clicked i want to highlight "Example" option background...
I tried with current_page? rails helper method, :complete attribute of link_to_remote but no luck can any one please suggest me on this. ... Thanks in advance
there are params[:controller] and params[:action] that help you to know which action is current.
<div id='example_link'>
<%= link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %>
</div>
in your home controller
def bank
# your code
render :update do |page|
page << "$('#example_link').addClass('highlighted')"
end
end
in css
.highlighted{
background-color: yellow;
}

Resources