Question regarding RJS and usage of link_to_remote - ruby-on-rails

I was working on my website when I cam across this problem, I have 100 posts in my blog and I am paginating them 10 at a time, I display the 1st 15 and display a link at the bottom which basically is implemented using the link_to_remote tag.
<%= link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'} %>
I click on that and get the next 15 posts, and append it to the container containing the 1st 15 posts through insert_html
page.insert_html :bottom, :puzzles , :partial => 'puzzles', :object => #puzzles
Now what I want is the link at the bottom of the page to update too, so that the next time the user clicks more the 3rd batch is fetched and so on. Basically something like
page.replace_html 'more-link', link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'}
Any clues as to how I can do it?

You're very close.
replace_html is called with (DOM_id, options for render). Essentially you want to render the output of a link_to_remote call. But you're not passing it in a form that render can use. As Barry Hess points out replace is much better suited for this task, because most of what you're changing is the tag attributes.
Using replace_html would result in nested tags which is could cause problems. You want to replace the element entirely. Replace has the same syntax of replace_html so you would run into the same problems if you were just switch replace_html to replace.
Here's what you want:
page.replace 'more-link', :text => link_to_remote "More Posts",
:url => {:action => 'view' ,:id => link.to_i + 1} ,
:html => {:id => 'more-link'}
However I'm not sure if link_to_remote is accessible from RJS. If the above doesn't work you could always do this:
page.replace 'more-link', :inline => "<%= link_to_remote 'More Posts',
:url => {:action => 'view' ,:id => link.to_i + 1},
:html => {:id => 'more-link'} %>", :locals => {:link => link}

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

how to paginate combined with anchor id

I'm very new to Ruby on Rails and learning while trying to fix some bugs on my company website. I'm trying to paginate a collection of records combined with a particular anchor, i.e. when user clicks on the next/previous page, the pagination happens and user lands on a particular section of the page. This is how my code looks at the moment:
view
<%= page_navigation_links #student_logs, :page %></p>
controller:
#student_logs.paginate(:page => params[:page], :per_page => 10)
application_helper
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
This works fine for my pagination, but I need to fit a reference to an 'anchor id' into this code, like Student Logs, so that when user navigates to a different page the browser navigates the user to the heading with id: "student_logs".
I can't figure out how to do this. Can anyone help?
I do not have direct experience with it. However, this link shows a way. I could not test but you can certainly do.
<%= will_paginate #posts, :params => {:anchor => i} %>
If this works, then you can utilize this way in your helper:
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :params => {:anchor => "#ANCHOR"}, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
You can replace #ANCHOR as you need.

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 " %>

Simple link_to_remote function - can't get it to work

After utilizing the great trial and error for over an hour along with dozens of tutorials and blogs posting examples, I still cannot get the simple ajax functionality to work. Here is what is in my partial:
<span id="friend">
<%= link_to_remote image_submit_tag("/images/add_as_friend.gif"), :url => {:controller => 'friends', :action => 'add', :id => id} %>
</span>
This is what is in my controller:
class FriendsController < ApplicationController
def add
unless params[:id].nil?
Friend.create(:user_id => #current_user.id, :friend_id => params[:id], :friend_type => 2)
end
end
end
This is what is in my add.rjs file:
page.replace_html 'friend', "A request to be friends with this player has been sent."
The image for the link comes up fine. When I click on it, however, nothing happens. I've checked the database and nothing is going on. I must be missing something, any thoughts?
It may be because you are using image_submit_tag rather than image_tag for the content of your link. image_submit_tag is for submitting forms so will have its own onclick behaviour which may override the onclick behaviour that will be added as part of the link_to_remote functionality.
I would try:
<%= link_to_remote image_tag("/images/add_as_friend.gif"),
:url => {:controller => 'friends', :action => 'add', :id => id} %>

Ruby on Rails: link_to_remote and rel

I want a link to remote to have a rel tag, because I want to use facebox with it.
I had it working with a regular link to... but I needed the link to remote to handle the event that a user doesn't have javascript enabled.
this, currently does't work (except for the non-javascript part )
<%= link_to_remote "Ask a Question",
{:url =>
{:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}, :rel => 'facebox'},
:href => url_for(
:controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true) %>
In link_to_remote you pass in HTML options (like rel) in the third argument. In your code you're passing it in the second (i.e. the first hash). Try this instead:
<%= link_to_remote("Ask a Question",
{ :url => { :action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => false
}
},
{ :href => url_for( :controller => :view,
:action => :ask_question,
:id => #container.id.to_s,
:javascript_disabled => true ),
:rel => 'facebox'
}
)
%>
(As you know, some of the parentheses and curly braces are optional, but here I've included all of them for clarity, and probably would keep them in since you're passing a lot of complex arguments here.)

Resources