View
<%= link_to "Link", {:action => "AjaxView",:col => "colname"},
:update => "Ajaxcall", :remote => true %>
Controller
def AjaxView
#vars= Var.find(:all,:conditions => { :varName=> "one" },:select=>(params[:col]))
respond_to do |format|
format.js { render :layout=>false }
end
end
AjaxView.js.erb
if ( (<%= col %>) == "colName") {
$("#3").text("<%= escape_javascript(render(:partial => "var") %>");
}
else
{
$("#2").text("<%= escape_javascript(render(:partial => "var") %>");
}
Issue here is this Variable "col" doesn't get its exact value in AjaxView.js.erb file, and my If else condition doesn't get executed. What am i missing here ?
The local variable col isn't assigned in AjaxView.js.erb. I think you meant to write it as params[:col].
Also the double quotes around var should be single quotes, to play nice with the surrounding double quotes.
{:action => "AjaxView",:col => "colname"}
This is not how you build a url for link_to.
What's the route to access the AjaxView method?
It should be something like: link_to "Link", AjaxView_resources_path(:col => "colname").
And in your controller, you need to set an instance variable #col = params[:col] to be able to use it in your view.
ps: your should name your method ajax_view instead of AjaxView.
if ( #col.to_s == "colName") {
$("#3").text("<%= escape_javascript(render(:partial => "var") %>");
}
elsif ( #col.to_s == "colName1")
{
$("#2").text("<%= escape_javascript(render(:partial => "var") %>");
}
Related
I want to pass a parameter using link_to. (Also I am trying to use Bootstrap tab)
ilban.html.erb
<%= link_to '일반공지', '#home', { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab', :where => 1 } %>
cpu_controller.rb
#where = params[:where]
This code doesn't get where as an parameter. How can I fix it?
I have not tested it, but it should pass the params that you want.
link_to "Search", searches_path(:where => 1, :when => "Today"), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
Controller:
#where = params[:where]
In Rails 5, try this syntax for link_to
link_to 'Show', view_path(:id => view.id), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
In the place of view path you can edit with your controller path and pass the valid id that you need to link.
Or, try this syntax also to pass params
<%= link_to "Add car", {:controller => "car", :action => "add_car", :car => car.id }%>
And add in your controller
#car = Car.find(params[:car])
Hello I am trying to retrieve html partials as a single string in an ajax call. I am using public_activity gem. Since I didnt find a way to call the partial(render_activity) from the controller. I tried using the following code, but I am getting an error saying that I can not make more than one render call. Any ideas of how can I achieve what I want?
#new_feeds.each_with_index do |new_feed|
if new_feed.trackable_type == "CompanyDocument"
if new_feed.key == "company_document.create"
html = render partial: 'public_activity/company_document/create', :locals => { :activity => new_feed }
elsif new_feed.key == "company_document.update"
html = render partial: 'public_activity/company_document/update', :locals => { :activity => new_feed }
end
elsif new_feed.trackable_type == "CompanyVideo"
if new_feed.key == "company_video.create"
html = render partial: 'public_activity/company_video/create', :locals => { :activity => new_feed }
elsif new_feed.key == "company_video.update"
html = render partial: 'public_activity/company_video/update', :locals => { :activity => new_feed }
end
end
end
Replace all occurrances of render with render_to_string
I'm making a custom AJAX form.
If a user fills out half of the form, it will fail wanting the other half fulfilled.
For some odd reason, the form is not pre-populating with at least what they had already inputted.
Because it's not doing this, how can I manually tell it to do so? When the form returns, the Object it verified is still accessible, so I can withdraw information from it.
a sample of the form
- form_for CardSignup.new do |f|
- unless #card_signup.nil?
- if !#card_signup.errors.empty?
.prefix_1.grid_4
- #card_signup.errors.full_messages.each do |error|
.warning
= error
%br/
.grid_2.prefix_1{:style => "width: 166px;"}
= f.text_field :first_name, :style => "width: 166px;", :value => "first name", :rel => "first name"
%div{:class => 'error_message'}
my Create method
def create
if params[:user] && current_user.admin
#card_signup = User.find(params[:user]).build_card_signup(params[:card_signup])
else
#card_signup = current_user.build_card_signup(params[:card_signup])
end
if #card_signup.valid?
respond_to do |wants|
#wants.html { redirect_to disclaimer_card_signup_path, :locals => { :card_signup => #card_signup } }
wants.json { render :json => { :html => (render_to_string :partial => 'disclaimer') } }
end
else
respond_to do |wants|
#wants.html { redirect_to new_card_signup_path }
wants.json { render :json => {:errors => #card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
end
end
end
You're doing this:
form_for CardSignup.new
which will instantiate a new CardSignup object every time you render the form. Move this into the new action and in the create action do this:
#card_signup = CardSignup.new(params[:card_signup])
This will pass the half-filled object back to the form and you'll get the already populated fields.
I'm trying to implement a link, when clicked marks you as present for a meeting. This link is a method in a helper:
def link_to_remote_registration(event_id)
down_image = "down_blanco.png"
up_image = "up_blanco.png"
unless registration.nil?
if registration.present == 1
up_image = "up_filled.png"
elsif registration.present == 0
down_image = "down_filled.png"
end
end
link_to_remote_registration = String.new
loading_and_complete = "Element.show('indicator_event_"+event_id.to_s+"'); Element.hide('vote_"+event_id.to_s+"')".html_safe
complete = "Element.hide('indicator_event_"+event_id.to_s+"'); Element.show('vote_"+event_id.to_s+"')".html_safe
link_to_remote_registration =
link_to(image_tag(up_image , :id => 'will_not_attend_event_'+ event_id.to_s , border => 0),
:url => new_registration_path(:present => 1, :event_id => event_id, :escape => false),
:remote => true,
:method => :put,
:loading => loading_and_complete,
:complete => complete)
return link_to_remote_registration
end
The problem is that when I render the link in my view some of the html gets escaped making the link not work.
<a href="/calendar?complete=Element.hide%28%27indicator_event_1%27%29%3B+Element.show%28%27vote_1%27%29&loading=Element.show%28%27indicator_event_1%27%29%3B+Element.hide%28%27vote_1%27%29&method=put&remote=true&url=%2Fregistrations%2Fnew%3Fevent_id%3D1%26present%3D1">
<img id="will_not_attend_event_1" border="0" src="/images/up_blanco.png?1198181114" alt="Up_blanco">
</a>
Which I think is not a valid url. I wonder why this happens - i call the html escape on the complete and loading string.
Regards
Since you're passing the html from from a helper, Rails sanitizes it to protect from XSS. You can override it by returning:
link_to_remote_registration.html_safe
http://railscasts.com/episodes/204-xss-protection-in-rails-3
You could also use raw() instead of disabling XSS system-wide.
raw(image_tag(up_image , :id => 'will_not_attend_event_'+ event_id.to_s , border => 0))
In my controller I have:
- #items.each do |item|
= render :partial => 'item', :locals => { :item => item, :draggable => true }
And in the item partial I have:
%span{:id => "item_#{item.id}", :class => 'item'}
= item.name
- if defined?(draggable)
= draggable_element "item_#{item.id}", :revert => true
This is not working, however, because defined?(draggable) returns false. The draggable_element is never rendered.
I know that item is passed through :locals because the rest of the partial renders. If I change the partial to read:
- if defined?(item)
= draggable_element "item_#{item.id}", :revert => true
Then the draggable_element is rendered.
Any idea why :draggable is not getting passed to the partial?
Use local_assigns[:draggable] instead of defined?(draggable).
From the Rails API "Testing using defined? var will not work. This is an implementation restriction."
I've solved this problem in the past by throwing this into the top of the partial.
<% draggable ||= nil %>
That allows me to do
<% if draggable %>
So long as I don't try to make the distinction between draggable being nil and never being passed.