Using Ajax with Rails link - ruby-on-rails

I am using link_to tag to change the validity:
<%= link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"),
:action =>'change_validity',:id => doc.id %>
Here, is_valid is a field in a table with boolean value. When it is true link will show
as "Mark as invalid". When I click the link it will call the method "change_validity" method
in controller. The method will toggle the is_valid field and show "Mark as valid" in view.
This one I want to do using AJAX. I tried to using link_to_remote. But I couldn't get it.
Can anyone explain how to do it???

Make one partial page.
_preview.html.erb and put below code into your partial view
<%= link_to_remote "Mark as " + (doc.is_valid ? "invalid" : "valid"), :update => "update", :url => { :action => "change_validity", :id => doc.id } %>
In your main view file.put below code
<div id="update">
<%= render :partial => "preview", :locals => { :doc => #doc} %>
</div>
In your controller should have below code
def change_validity
// do stuff here
render :partial => "preview", :locals => { :doc => #doc}, :content_type => 'text/html'
end

link_to_remote is not available in Rails 3. Add :remote => true to your link.
link_to "Mark as " + (doc.is_valid ? "invalid" : "valid"),
change_validity_path(:id => doc.id), :remote => true
EDIT: for rails < 3 try
link_to_remote(
"Mark as " + (doc.is_valid ? "invalid" : "valid"),
:url => {:action => "change_validity", :id => doc.id},
:update => "your_div_id",
:html => {:class => "something"}
)

Related

How to pass a parameter from view to controller

I have an app in ruby on rails with a view which has some parameters displaying on screen.
<%= link_to l(:label_statistics),
{:action => 'stats', :id => #project, :repository_id => #repository.identifier_param},
:class => 'icon icon-stats' if #repository.supports_all_revisions? %>
I'd like to catch the variable #repository.name and use it in the function I have in my controller.
How do I to do it?
In your stats action, assuming you have a Repository model and the record containing name exists in your database.
#repository = Repository.find(params[:repository_id])
#repository.name
If not, you could pass it along with everything else,
<%= link_to l(:label_statistics),
{:action => 'stats',
:id => #project,
:repository_id => #repository.identifier_param,
:repository_name => #repository.name},
:class => 'icon icon-stats' if #repository.supports_all_revisions? %>
Then in the controller, access it via params[:repository_name]
Just pass it through the parameters. Doc for li_to.
link_to can also produce links with anchors or query strings:
link_to "Comment wall", profile_path(#profile, :anchor => "wall") # => Comment wall
link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails" # => Ruby on Rails search
link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux") # => Nonsense search

Link_to remote => true causing strange routing problems, Rails 3

I am having some problems with link_to remote sending a post to another controller... The result are not quite what I expect..
I have this in node_content.html.erb:
<% #node.videos.each do |vid| %>
<div id="vid_vid"><%= link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"), :controller => 'videos', :action => "iframize", :video_id => vid.id, :method => :post, :remote => true %></div>
<% end %>
And I have this in videos_controller:
def iframize
#video = Video.find(params[:video_id])
respond_to do |format|
format.js
end
end
And this in routes:
resource :videos do
collection do
post 'iframize'
end
end
Problem is that when I click the link, it takes me to
http://localhost:3000/videos/iframize?method=post&video_id=20
and I get
Couldn't find Video with id=iframize
I looked through tens of various examples and they seem to recommend the above, but it does not work.. What am I doing wrong?
Any input greatly appreciated!
Thanks!
EDIT:
I tried this approach jquery function and it kinda worked (only for the first video in the loop of course):
<% #node.videos.each do |vid| %>
<%= image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg", :id => 'img_div') %>
<div id="vid_vid"> <%= vid.id %></div>
<% end %>
$('#img_div').on({
'click': function() {
var vid_id = document.getElementById("vid_vid").innerHTML;
$.post("/videos/iframize/", {video_id: vid_id});
}
});
Do you have rails_ujs included via jquery_ujs in assets/application.css file
= link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"), "url_path", :method => :post, :remote => true
Convert the following into a rails path: like iframeize_videos_path (generated via rake routes)
:controller => 'videos', :action => "iframize", :video_id => vid.id,
You need to differentiate the url options, html options.so use
<%= link_to image_tag("http://img.youtube.com/vi/#{vid.content}/1.jpg"),
{ :controller => 'videos', :action => "iframize", :video_id => vid.id },
{ :method => :post, :remote => true} %>

How can I pass variable with Ajax link in Rails?

I want to pass variable with link_to_remote in Rails 2.3. Following is my code for passing variable. But controller did not get that variable. Anybody can help me ?
<%= link_to_remote 'Add new event', :url => {:controller => 'events', :action => 'new' }, :with=> 'event' %><br>
Any hash key you add that's not part of the keyword set (controller, action, format) will be appended to your URL as an argument.
ie.
<%= link_to_remote "Add New Event", :url => {:controller => 'events', :action => 'new', :var => 'event'} %>
Would yield
/events/new?var=event
Hope this would solve your problem,I have not tried.
<%= link_to_remote "Add New event",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>

Trouble passing locals in Partial

I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id},
:multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete",
:id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar,
:title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #<ActionView::Base:0x2736bb0>
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Try using :id=>#entity_id (note the # symbol preceding the name). Likewise, #title, instead of "title".

switch images with onclick

I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id}, :multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete", :id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar, :title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Are you sure the error is coming from the partial? You are using entity_id in the form_tag. Where is it being defined? title isn't used in the partial. It is used inside of legend though. Is that defined?

Resources