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;
}
Related
I am redirecting a user to a custom controller action like this:
<%= link_to url_for(:controller => :jobs, :action => :saved) do %>Saved Jobs<% end %>
it works fine, now i want to pass some value to action "saved".
Do you have any idea how to do this?
Try This
If you want to send the form directly by reloading the page as you are doing,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value") do %>Saved Jobs<% end %>
If you want to send it through ajax call from background, add :remote => true to the form,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value", :remote => true) do %>Saved Jobs<% end %>
Then in controller
#email = params[:email]
I am working in rails 2. I want to make an link_to_remote object and render it on html page. I want to send date via ajax. I am trying this.
render :text => "<font color='"+ color +"'>" +params[:ajax_status] + "</font>" + <%= link_to_remote '| Undo',
:update => 'status_'" +params[:ajax_status]+ ",:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => "+ params[:id] +" :ajax_status => 'Undo'} %>
But unable to proceed. It shows on webpage
Accepted<%= link_to_remote '| Undo', :update => 'status_'Accepted,:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => 20 :ajax_status => 'Undo'} %>
PLease help me out.
You don't need the <%= %> tags, in this case - they're for erb.
Try just:
render :text => "#{ content_tag :font, params[:ajax_status], :color => color }#{ link_to_remote '| Undo', :update => "status_#{ params[:ajax_status] }", :url => {:controller => 'requests', :action => 'action_on_punching_request', :id => params[:id], :ajax_status => 'Undo'} }"
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
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!
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 %>