I'm using meta_search gem.I have url like this for admin projects index page with search parameters.
admin/projects?utf8=✓&search%5Bid_equals%5D=&search%5Btitle_contains%5D=&search%5Bstage_in%5D=completed
Then user choose one project and url will be this
admin/projects/a--15/edit?page=1
When user update this form,The search parameters will be lost.
How can i keep these parameters.I mean with session or meta_search have some method to fix this?
First, create a filter that fires for every action that might want access to the search parameters:
ProjectsController < ApplicationController
before_filter :save_searches
def save_searches
#addons = ''
[:page, :id_equals,:title_contains,:stage_in].each do |k|
if params[k]
pval = params[k].is_a?(Array) ? params[k].join(',') : params[k]
#addons << k.to_s + "=" + pval + "&"
end
end
#addons.chop!
end
Now, when your actions fire, #addons will be set, and then you can do this:
<%= link_to 'Edit' , edit_path(#project.id) + #addons.length > 0 ? "?" + #addons : '' %>
That said, I'm willing to bet this is somewhat of a hack, and there is a cleaner way to do it. But this works for me.
The :page key makes it so that if you have pagination running and using the :page param to track current page, your pagination should be remembered as well.
Also note that in the event of you getting an Array in your params, (i.e. the result of a select with :multiple=>true), this is handled.
Related
I followed http://railscasts.com/episodes/228-sortable-table-columns to make a sortable table on my records index.erb
So everything works good for that, but now I've gone back to my homepage and put this code in the index.erb for my home object:
<%= link_to "Records Index", records_path %>
And I get a sql error:
So anyway, I went back to records page that was working and saw that my application_helper.rb was doing this to the URL
http://localhost:3000/records?direction=asc&sort=firstname
So now a simple records_path call doesn't work. What would work instead? I tried
records_path & "?direction=asc&sort=firstname"
but that didn't help either. can you help me? thanks!
I thought perhaps I should fix the Def for index in the records controller so I was trying stuff like this
class RecordsController < ApplicationController
...
def index
if sort_column.blank? #sort_direction.nil? || sort_column.nil?
#records = Record.all
else
#records = Record.order(sort_column + " " + sort_direction)
end
end
but that didn't work. so I tried this on the home page:
<%= link_to "Records Index", '\records?direction=asc&sort=firstname' %>
I'm certain there's a much better way to do this using the routes or correcting something in application helper or records controller but this'll work for me.
I need to add a few links to certain pages of the application in the ActiveAdmin pages. I can do this using sidebars, but I'll have to repeat the code for each of my resources. Is there anyway of adding custom links to the header ? Or define a sidebar that will appear for all resources ?
I also wouldn't want to overlook setting config.site_title_link in initializers/active_admin.rb.
I'm pretty sure it takes a symbol representing the name of a route from your application, for example:
config.site_title_link = :root
would link the site title to your application's root_path.
Thanks #phoet ! Implemented it by overriding the HeaderRenderer instead:
module ActiveAdmin
module Views
class HeaderRenderer
def to_html
title + global_navigation + application_link + utility_navigation
end
def application_link
link_to('Back to Application', root_url)
end
end
end
end
i think there is no build-in way to do it, but you can override the render-logic in the TabsRenderer (2.2) / TabbedNavigation (3.0):
def render_menu(menu)
content_tag :ul, :id => #options[:id] do
menu.items.collect do |item|
render_item(item)
end.join.<<('your_custom_stuff').html_safe
end
end
I am currently trying to define my first new action in Rails 3, despite various problems I think things are almost done now. However, I as a final hurdle I am struggling to send the correct parameter value to my function...
My function is defined in items_controller:
def clickcountplusone
clickeditem = Item.find(params[:id])
redirect_to clickeditem.externalurl if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
end
routes.rb contains:
match '/items/clickcountplusone', :to => 'items#clickcountplusone'
and the view contains:
<%= link_to image_tag( item.picture.to_s + ".gif", send("items_clickcountplusone_path", item.item_name)%>
The view page itself loads correctly (the one with the link on it), but when I click on the link I get an error:
Couldn't find Item with ID=clickcountplusone
{"id"=>"clickcountplusone",
"format"=>"whatever the name is"}
and rather than going to the external page, my browser tries to load:
http://localhost:3000/items/clickcountplusone.whatever the name is
Can anyone tell me how I should be calling the function so that the ID is the item_name and the external URL is visited rather than an incorrect one on my site?
Thanks in advance
It seems like this would be a normal route, instead of a RESTful route (this is fine). There are some places you have to change.
First, in your controller's action, you used params[:id] which is not set actually.
In this particular case, I would suggest you use params[:item_name] instead of id because you are really sending the item_name.
def clickcountplusone
clickeditem = Item.find_by_item_name(params[:item_name])
redirect_to clickeditem.externalurl if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
end
Item.find could only be used if the parameter is one of the actual id / :all / :first / :last.
You are finding by the item_name, so you should use Item.find_by_item_name instead.
Second, you have to update you route too (or else you would need something like /you_path..?item_name=blahblahblah which is fine too if you don't mind)
get 'items/:item_name' => 'items#clickcountplusone', :as => :items_clickcountplusone
Third, you view. IMO, most of the time if you are using send but not writing library / really back end code, you probably misusing it.
<%= link_to image_tag("#{item.picture.to_s}.gif"), items_clickcountplusone_path(:item_name => item.item_name) %>
You don't have any variable in your match statement. Try something like
match '/items/clickcountplusone/:id', :to => 'items#clickcountplusone'
and
<%= link_to image_tag(item.picture.to_s + ".gif", items_clickcountplusone_path(:id => item.item_name))%>
I have this method that puts the links of the 10 results from the Google custom search API into an array:
require 'json'
require 'open-uri'
def create
search = params[:search][:search]
base_url = "https://www.googleapis.com/customsearch/v1?"
stream = open("#{base_url}key=XXXXXXXXXXXXX&cx=XXXXXXXXXX&q=#{search}&start=#{i}&alt=json")
raise 'web service error' if (stream.status.first != '200')
result = JSON.parse(stream.read)
#new = []
result['items'].each do |r|
#new << r['link']
end
end
and my view:
<% #new.each do |link| %>
<p><%= link %></p>
<% end %>
I'm having trouble figuring out how to add pagination with this so that on the second page would return the next 10 results. I'm using the Kaminari gem for pagination.
I want for when a user clicks a link to another page, I fetch the next 10 results from Google's API. You can do this with the API's start parameter that specifies the first result to start with, which I pass as i. I was thinking of doing something like this:
i = (params[:page] - 1) * 10 + 1
where params[:page] is the current page number, but for some reason it is undefined. Also I'm unsure about how to setup pagination for an array that is not an AR object, and what would go in my view. I'd appreciate any help, and feel free to use any pagination gem you know.
How are you setting params[page]? It needs to be passed in with the other parameters in your request in some way.
Perhaps you need something like this in your controller:
#page = params[:page] || 1
i = (#page - 1) * PER_PAGE + 1
stream = open("#{base_url}key=XXXXXXXXXXXXX&cx=XXXXXXXXXX&q=#{search}&start=#{i}&alt=json")
raise 'web service error' if (stream.status.first != '200')
result = JSON.parse(stream.read)
#new = result['items'].map{|r| r['link']}
In your view you need to make sure that you are passing the page via the query parameter in the link to fetch the next set of results. Most likely that you would want to return #page + 1.
Handling pagination with non ActiveRecord objects depends on your pagination library. You might want to check out how will_paginate handles this.
From the list view of my app, I can view a list of records or drill down and edit/update a record. After updating, I want to go directly back to the list view, bypassing a couple of intermediate pages - but I don't simply want to link_to(:action => list) - there's pagination involved. I want to go back to the exact 'list' page I came from. What's the best way? Pass a hidden arg somewhere with the page number? Is there an elegant way to accomplish this?
I'm just going to throw this one out there with the disclaimer that there may be security considerations or existing gems.
On your edit action, you could store the previous page in a session. Then in your update action, redirect to it.
class MyController < ApplicationController
def edit
session[:prev_url] = request.referer
end
def update
redirect_to session[:prev_url]
end
end
As an alternative to use the session, you could carry the referer through the actions using a hidden form field.
class MyController < ApplicationController
def edit
#prev_url = request.referer
end
def update
redirect_to params[:prev_url]
end
end
Form using hidden_field:
f.hidden_field :prev_url, :value => #prev_url
If you do not want to carry along the whole referer url you could also do the same with the page parameter instead and append the parameter to the url in the update action. I would also expect Rails' url helpers to accept parameters.