In Active Admin (RoR), I'm getting data from a table, no problem with that.
But I want to add a sidebar with information thats related with an inner join.
But I don't know how to do this.
This my code:
ActiveAdmin.register Project do
show do |project|
... works ...
end
end
sidebar "Resources for this project", :only => :show do
table_for project.project_resources.where('project_id = ?', project.id) do |row|
column "Resource", Resource.where('id = ?', :resource_id) do |resource|
:resource_name
end
end
end
As you can see, I'm trying to get the resource_name from resources-table. There is a 3th table, project_resource and this contains all the resource_ids for a project_id.
Thanks
Ok, this is the answer I was looking for:
sidebar "Resources for this project", :only => :show do
table_for project.resources do |resource|
column :resource_name do |row|
link_to row.resource_name, admin_resource_path(row)
end
end
end
Related
I want previous/next buttons on my edit screen for each record that send you to the previous/next record ordered by id.
None of the simple solutions I have found online have worked and I can't seem to figure out why.
controller:
def edit
#school = School.find(params[:id])
end
model:
def previous_school
School.where(['id < ?', id]).last
end
def next_school
School.where(['id > ?', id]).first
end
view:
<%= link_to("Previous School", #school.previous_school) if #school.previous_school %>
<%= link_to("Next School", #school.next_school) if #school.next_school %>
routes:
get 'school' => 'schools#edit'
When I try pressing the buttons using this code, instead of sending me to where I want to go: "admin/schools/:id/edit", I get sent to: "school.:id" and I'm not sure why.
You'd better use resources for describing routes:
resources :schools
Then your view should be
<%= link_to("Previous School", edit_school_path(#school.previous_school.id) if #school.previous_school %>
Activeadmin layout is not just a file, its a collection of components.
How can I override some of the components like, logo, navigation with activeadmin.
The activeadmin layout components are very easy to customize. What you need to do: Just define a module that opens ActiveAdmin::View.
you can have a custom_activeadmin_components.rb in initializers or admin directory where you defined all of your activeadmin resources. I prefer to put it in the directory where your activeadmin resources are. Then override any module you want:
here is a sample:
module ActiveAdmin
module Views
class Header < Component
def build(namespace, menu)
super(:id => "header")
#namespace = namespace
#menu = menu
#utility_menu = #namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
build_utility_navigation
#you can add any other component here in header section
end
def build_site_title
render "admin/parts/logo"
end
def build_global_navigation
render "admin/parts/main_nav"
end
def build_utility_navigation
render 'admin/parts/language_options'
insert_tag view_factory.global_navigation, #utility_menu, :id => "utility_nav", :class => 'header-item tabs'
render 'admin/parts/branch_in_header'
end
end
module Pages
class Base
def build_page_content
build_flash_messages
div :id => :wizard_progress_bar do
render 'admin/parts/wizard_progress_bar'
end
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
end
end
end
end
You can customise active admin page in admin/your_model.rb file.
Sample code for active admin is as follows.
ActiveAdmin.register User do
menu :label => "Our User", :priority => 3 #rename menu & set priority#
#for index page#
index :title => 'Our User' do #set page title#
# index :download_links => false do
selectable_column
column :title
column :category do |e| #want to change name of category
e.categoryone
end
column :address
default_actions#this will add default action i.e. show|edit|delete#
end
#end index#
#for controller#
controller do
actions :all, :except => [:edit, :new] # you can decide which all methods to be shown in show page.
end
#end controller#
#show start page#
show do |user|
h3 "User Details"
attributes_table do
row :title
row :description
row :address, :label=>"User Address" #overide address label
row :country
row :approval
end
h3 "Activity Photoes"
attributes_table do
user.uploads.each do |img| #call associated model. Here i want to display uploaded image of upload.rb file
row(" ") do
link_to image_tag(img.to_jq_upload['small_url']), admin_upload_path(img)#here i have added upload module to admin thats y i can directly give path to the upload record of admin module#
end
end
end
active_admin_comments # to show comment block of active admin
end
#show end#
#filer(search) start righthand side panel#
filter :title
filter :category
filter :address
#end filter#
#for menu on show page#
action_item only:[:show] do # this add "Approve User" button on the right hand side of show menu only as we have specified only show method
if User.find(params[:id]).approval == true
link_to "Approve User", approv_user_path(:id => params[:id])#call custom method of normal rails controller#
end
end
#end menu#
#start add side bar on page#
sidebar "Project Details" do
ul do
li link_to("Profile", new_project_path)
end
end
#end side bar#
end
I'm fairly new to rails & I've been stuck on this problem for some hours. I am trying to display the posts the user has liked and the user's actual posts.
With my current code, I'm getting this error "undefined method `title' for #" and it's being extracted from this line: "<%= link_to post.title, post %>".
Coud anyone shed some light as to how I can get this to work? (more code below)
def show
#user = User.find_by_username(params[:id])
if #user
#posts = #user.posts.all + #user.likes.all
render actions: :show
#likes = #user.likes.all
else
render file: 'public/404', status: 404, formats: [:html]
end
end
Here's my routes file:
resources :likes, only: [:create, :destroy]
resources :posts
devise_scope :user do
get 'register', to: 'devise/registrations#new'
get 'edit', to: 'devise/registrations#edit'
get 'login', to: 'devise/sessions#new'
get 'logout', to: 'devise/sessions#destroy'
end
Here's the 'show' view:
<% if #posts %>
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<% end %>
<% end %>
"<%= link_to post.title, post %> display <%= post.inspect %>" here's the results
<Post id: 11, title: "testing123", user_id: 2, created_at: "2013-05-18 19:25:45", updated_at: "2013-05-18 19:25:45"> #<Like id: 23, post_id: 10, user_id: 2, created_at: "2013-05-18 21:39:17", updated_at: "2013-05-18 21:39:17">
It works when I just use this -> #posts = #user.posts.all but the problem starts when I use this -> #posts = #user.posts.all + #user.likes.all
which gives me the "undefined method `title'" message..
You may want to add something to the user model...
class User < ActiveRecord::Base
has_many :likes
has_many :liked_posts, :through => :likes, :source => :post
...
end
Then, in your controller you can do this
#posts = #user.posts + #user.liked_posts
I think that in active record it will be faster than using ruby map
#posts = Post.joins("left join likes on likes.post_id = posts.id").
where("posts.user_id ? OR likes.user_id = ?", #user.id, #user.id)
Probably better will be to define scope in post model
scope :owned_or_liked_by, ->(user) { joins("left join likes on likes.post_id = posts.id").
where("posts.user_id ? OR likes.user_id = ?", user.id, user.id) }
And then you can use
#posts = Post.owned_or_liked_by(#user)
Other solutions will also work, but that generates you one sql query and it will be faster when you will have a lot amount of data.
It seems like #user.likes# doesn't return a Post related Array or ActiveRelation.
If assuming that a Like object has a post_id attribute, then you might want to modify the code as such:
#posts = #user.posts.all + #user.likes.map(&:post)
This is another approach you can take, a way to put it into one query and potentially sort. If you want to include it all in one list.
class Post < ActiveRecord::Base
def self.visible_to(user)
where('posts.user_id = :user_id or likes.user_id = :user_id', { :user_id => user.id}).includes(:likes).order('created_at desc')
end
end
Then in the controller, you'd do
#posts = Post.visible_to(#user)
My Active admin version is 0.3.0, other than that m also using 'Will_paginate' i had made the configuration settings of conflict between kaminari and will_paginate but still m getting this error. i dont know where m making a mistake everything is working fine for other model but not for this model, need help, i have searched also found some links but not get satisfactory answer, it give me error on bold line
ActiveAdmin.register User do
menu :parent => 'Reports'
filter :id
filter :user_id
filter :updated_at
# and other filters
scope :all, :default => true do |user|
User.all
end
scope :active do |user|
User.where("user.status = ?", 'actvie')
end
scope :rejected do |user|
User.where("user.status = ?", 'non_active')
end
actions :index, :show
index do
column "ID" do |u|
link_to u.id, cs_user_path(u)
end
column "Status" do |u|
status_tag(u.status)
end
column "User" do |u|
link_to(u.user.full_name, cs_user_path(u.user)) rescue nil
end
end
collection_action :index, :method => :get do
scope = User.includes([:group,:address]).scoped
scope = scope.order params[:order].gsub(/_/,' ') if params[:order]
#collection = scope.paginate(:per_page => 25,:page => params[:page]) if params[:q].blank?
#search = scope.metasearch(clean_search_params(params[:q]))
**super do |format|**
format.html {
render "active_admin/resource/index"
}
end
end
end
The GitHub documentation of will_paginate states that arrays are not supported that well.
I will suggest using the kaminari gem which includes a helper method for array pagination.
You should be able to take it from here.
I have such problem: I don't need to nest all resources, because I need only two action.
First - show all reports related with 1 website.
Second - show all reports.
When I rewrite code in routes.rb like this:
resources :websites do
member do
match 'performance_reports' => 'performance_reports#index'
match 'performance_reports/show' => 'performance_reports#show'
end
end
//rake routes
performance_reports_website /websites/:id/performance_reports(.:format)
performance_reports#index
performance_reports_show_website /websites/:id/performance_reports/show(.:format)
performance_reports#show
Here is my action:
before_filter :load_website
private
def load_website
#website = Website.find(params[:website_id])
end
def index
#performance_reports = #website.performance_reports
end
Code from view:
<%= link_to "Link", performance_reports_website_path(website)) %>
but when I'm calling it I get error:
Couldn't find Website without an ID
rb:7:in `load_website'
What I'm doing wrong ?
In controller change the code to
#website = Website.find(params[:id])