How can I pass variable with Ajax link in Rails? - ruby-on-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 " %>

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 using PUT method syntax to a different controller in rails 3

I try to call an action of my Gallery controller from a Portfolio view. A Portfolio is made of many galleries.
I try this:
<%= link_to("Heart", gallery_path(gallery), :action => "like", :method => :put , :remote => true) %>
<%= link_to("Heart", :controller => :galleries, :action => "like", :method => :put , :remote => true) %>
And I obtain:
Heart
and
Heart
I want to get but i m stuck...:
Heart
Any RAILS God to help me ?
I believe you're getting behaviour because you're trying to use the "path helper" and "params hash" styles in the same link_to (see the docs for more details). I prefer the path helper style, so I'd write the link like this:
<%= link_to(
'Heart',
like_gallery_path(gallery),
{:method => :put, :remote => true}
) %>
If you like the params hash style, you'd write:
<%= link_to(
'Heart',
{:controller => 'galleries', :action => 'like', :id => gallery.id},
{:method => :put, :remote => true}
) %>
Note that the URL parameters (controller, action, etc.) are in a separate hash from the link parameters (method & remote).
Hope that helps!
Try with:
<%= link_to "Heart", gallery_path(gallery), :url => { :controller => "galleries", :action => "like"}, :method => :put, :remote => true) %>

Using Ajax with Rails link

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"}
)

rails form_tag redirecting to /assets

i was wondering why does
<%= form_tag( { :action => "/search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
give the following error?
No route matches [POST] "/assets"
i notice it's because of the /search. if i rewrote the code as...
<%= form_tag( { :action => "search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
without the /search, it correctly calls my controller method. can someone explain why? thanks
This will do what you mean:
<%= form_tag( "/search", :method => "get" , :class => "span4" ) do %>
...
<% end %>
If the first parameter of form_tag is a hash as you have given, it is passed behind-the-scenes to url_for, which inteprets :action as the action part of a route for it to reverse-map.
Since you (I guess) want to just pass a plain URL, just pass it as a string for the first arg.
More info here of course :)
the ":action" should receive a name of "action", such as: "create", "update", or "destroy"
:action => "/search"
here the "/search" is not a name, but an "url", which is not legal.
btw, I suggest you use xx_url instead of { :action => "", :controller => ""}, e.g.
<%= form_tag "/search", :method => "get" %>
or
# you defined "search_path" in config/routes.rb
<%= form_tag search_path, :method => "get" %>

Using my own HTML5 data attributes in a Rails link_to helper

I have a link_to helper that I'd love to use Twitter's Bootstrap.js to provide a popover. I have the following code:
"access", :action => "login" %>
I need to add the following HTML attributes: data-placement="" and rel="".
How do I do this using link_to? I've tried different variations of this:
<%= link_to "Login", 'data-placement' => 'below', :controller => "access", :action => "login" %>
<%= link_to "Login", html_options = {'data-placement' => 'below'}, :controller => "access", :action => "login" %>
<%= link_to "Login", :data => {'placement' => 'below'}, :controller => "access", :action => "login" %>
All these do is append things to my URL so it becomes:
http://localhost:3000/access/login?data-placement=below
I'm sure I've done this before!
-Jim
Apparently when using this format, I needed to specify the controller and action in a hash.
<%= link_to "Login", {:controller => "access", :action => "login"}, 'data-placement' => 'below' %>

Resources