I'm using this code:
ActiveAdmin.register_page "Dashboard" do
section "Recent Posts" do
table_for Post.order("id desc").limit(15) do
column :id
column "Post title", :title do |post|
link_to post.title,[:admin,post]
end
column :category,sortable: :category
column :created_at
end
strong (link_to "Show all posts")
end
end
and I get this error:
undefined method `section'
if I delete 'section' do-end part, then I get error for:
undefined method `table_for'
and so on...
Seems like I cant use any of active admin given methods, maybe I'm mising something? Any gems or something? I installed active admin gem using this setup:
gem 'inherited_resources', github: 'activeadmin/inherited_resources'
gem 'activeadmin', github: 'activeadmin'
gem 'devise', github: 'plataformatec/devise'
I'm using rails 5
I managed to transform my code and now it compiles without any errors.
ActiveAdmin.register_page "Dashboard" do
content :title => proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Recent Posts" do
table_for Post.order("id desc").limit(5) do
column :name do |post|
link_to post.title, [:admin, post]
end
column :created_at
end
strong (link_to "Show All Posts" , :posts )
end
end
end
end
end
I suppose my previously used syntax is old and not supported any more.
Related
It's been a while since I've needed anything but a form_for.
Now I'm trying to set up an admin area search where the search box is in the menu bar. But instead of POSTing to the results path it's reloading the existing page with GET params, including the authenticity_token.
So my set up:
Gemfile
source 'https://rubygems.org'
ruby '2.2.3'
gem 'rails', '4.2.3'
# And so on....
routes.rb
namespace :admin do
resources :results do #search results
collection do
get :display
end
end
# many more routes here
end
admin/results_controller.rb
class Admin::ResultsController < ApplicationController
def display
#results = Elasticsearch::Model.search(params[:query], [Article]).paginate(page: params[:page], per_page: 30)
respond_to do |format|
format.html # index.html.erb
end
end
private
# Don't think this is really relevant, adding out of desparation
def result_params
params.require(:result).permit(:query)
end
end
And in my universal admin bar:
<%= form_tag(display_admin_results_path, method: :post) do %>
<%= text_field_tag :query, params[:query], {class: "form-control", placeholder: "Search"} %>
<span class="input-group-btn"><%= submit_tag "→".html_safe, class: "btn btn-success" %></span>
<% end %>
Logs show no sign of a redirection, Chome console shows no javascript "bork."
Instead of submitting that form and getting sent to /admin/results/display or even /admin/results/display?query=blah I'm gettting
?utf8=✓&authenticity_token=j3w4dtBbFLzJzqWGZ9x4Q4GsUi%2FxmjYFrPjdzm8ccLKdxpOR0KwrX2hIAzXkR96cuTVgwG1sbYBKDdSO%2F3O6Wg%3D%3D&query=hello&commit=→
It's got to be something obvious... prepared for the 'doh' moment.
The problem is in your routes.
In your console write rails routes (or rake routes), and you will see that it expects get method for your display_admin_results_path. So you access your controller with GET method.
your routes:
display_admin_results GET /admin/results/display(.:format) admin/results#display
admin_results GET /admin/results(.:format) admin/results#index
POST /admin/results(.:format) admin/results#create
admin_result GET /admin/results/:id(.:format) admin/results#show
PATCH /admin/results/:id(.:format) admin/results#update
PUT /admin/results/:id(.:format) admin/results#update
DELETE /admin/results/:id(.:format) admin/results#destroy
You should implement your search query, so that it will use POST, and return corresponding view(which is defenetly not index).
Also, you render method searches the view with coresponding name, so you may want to create display.html.erb file, or specify the view you want to use in controller.
The goal is to give a user points when his comment gets an upvote. Now it just gives points when the comment is created(5 points) and doesn't count upvotes.
I've looked at this answer and according to it my code is correct. I'm using acts_as_votable gem for upvotes/downvotes.
gemfile
gem 'merit', '~> 2.1.1'
comments_controller
def upvote
#comment.liked_by current_user
render "update_likes"
end
point_rules
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, :on => ['comments#create'], :to => [:user], category: 'gold_points'
score 2, :on => ['comments#upvote'], :to => [:user], category: 'gold_points'
end
end
end
user model
class User < ActiveRecord::Base
has_merit
display points
<%= #user.points(category: 'gold_points') %>
I found the answer:
change
render "update_likes"
to
render :nothing => true
I'd like to add a custom filter field to "Add News" page in Redmine,
so that when I add a new news I could select group of users the email should be sent to.
The field itself is a list of Redmine User groups and every user is assigned to at least 1 of them.
Has anybody done this? Any suggestions would be appreciated
I've located the 3 files related to the issue:
/app/controller/news_controller.rb
/app/models/news.rb
/app/views/news/_form.html.erb
Environment:
Redmine version 2.2.1.stable.11156
Ruby version 1.8.7 (x86_64-linux)
Rails version 3.2.11
Environment production
Database adapter MySQL
Redmine plugins:
no plugin installed
So far I've done only 1 modification in Redmine, which sends added news to all registered users.
File: /app/modelsmailer.rb
Overview:
EDIT: Following your advice I moved mailer function to the controller:
def create
#news = News.new(:project => #project, :author => User.current)
#news.safe_attributes = params[:news]
#news.save_attachments(params[:attachments])
if #news.save
#news_added(#news)
if params[:group]
mail :to => GroupsUser.find(params[:group][:ids]).joins(:users).select("users.mail").compact,
:subject => "[#{#news.project.name}] #{l(:label_news)}: #{#news.title}"
else
render :new
end
end
end
But I'm getting error: NameError (uninitialized constant NewsController::GroupsUser): pointing to line
mail :to => GroupsUser.find
news_controller.rb:
def new
#news = News.new
#groups = GroupsUser.all
end
news/_form.html.erb:
<%= label_tag :group_ids "Groups"
<%= collection_select :group, :ids, #groups, :id, :name, {}, multiple: true %>
Edit:
I'm going to have to take a few guesses on what your controllers look like, but I'll give you something close. Based on the mailer function you provided, I'm assuming that was called out of the create controller after the News was saved. I would call the mail function after that. Something like this:
def create
news = News.new(params[:news]
if news.save
news_added(news)
send_mail_to_groups(params[:group][:ids]) if params[:group]
redirect_to ...
else
render :new
end
end
The mailing part should be removed from news_added
def news_added(news)
redmine_headers 'Project' => news.project.identifier
#author = news.author
message_id news
#news = news
#news_url = url_for(:controller => 'news', :action => 'show', :id => news)
end
in favor of its own new routine:
send_mail_to_users_by_group_ids(group_ids)
# Woo: Sent to all users, despite their email settings
mail :to => GroupsUser.find(group_ids).joins(:users).select("users.mail").compact,
:subject => "[#{#news.project.name}] #{l(:label_news)}: #{#news.title}"
end
You might want to add a where clause to only include active users.
I think that's about right. I'm doing it off the top of my head so there's probably a typo or error or two in there. Hopefully it points you in the right direction though.
I'm using the path helper methods to generate URLs in link_to, and they are returning URLs formated like this :
http://localhost:3000/tweets.4
when I was expecting them to be formated like this:
http://localhost:3000/tweets/4
Note how it is using a dot as the delimiter instead of the expected forward slash. The top link doesn't resolve to the correct view, it simply reloads the /tweets view. When I manually edit the URL to be like the bottom, it opens the correct /tweets/show/.
The closest thing I found in my online research was that people encountered this with wrongly nested routing statements - but I don't think I'm doing that here.
I would appreciate any help or pointers anyone can provide!
Here are the related source files and version information :
tweets/index.html.erb
<h1>Listing tweets</h1>
<% #tweets.each do |tweet| %>
<div>
<!-- creates path in format of /tweets.2 -->
<div><%= link_to tweet.status, tweets_path(tweet) %></div>
<!-- creates path in the format of /user.1 -->
<div><%= link_to tweet.user.name, users_path(tweet.user) %></div>
</div>
<% end %>
tweets_controller.rb
class TweetsController < ApplicationController
def index
#tweets = Tweet.all
end
def show
#tweet = Tweet.find(params[:id])
end
def new
#tweet = Tweet.new
end
def create
#tweet = Tweet.new(params[:tweet])
#tweet.user = User.last
if(#tweet.save)
redirect_to :root
end
end
def edit
#tweet = Tweet.find(params[:id])
end
def delete
end
end
routes.rb
Zombietweets::Application.routes.draw do
resources :tweets
root :to => 'tweets#index'
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.9'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
group :assets do
gem 'sass-rails', '3.2.3'
gem 'coffee-rails', '3.2.1'
gem 'uglifier', '1.0.3'
end
gem 'jquery-rails', '2.0.2'
I'm using Rails 3.2.9 and Ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]
Have you tried tweet_path and user_path ?
You want to access the show action. For that action, the model name must be singular in the *_path call.
To be sure, try a rake routes in a console.
EDIT:
You also forget to add resources :users in your routes file :)
I have an active admin resource like this:
ActiveAdmin.register Snippet do
menu label: "Text Snippets"
config.clear_sidebar_sections!
index download_links: false do
column :key if current_admin_user.developer?
column :description
column :contents
default_actions
end
form do |f|
f.inputs do
f.input :description
f.input :contents
end
f.buttons
end
end
Notice in the index block, I'm only adding the key column if the current admin user is a developer. I want to apply this kind of filtering to the available actions.
I tried added this at the top of the resource definition:
actions current_admin_user.developer ? :all : :index, :edit
But I get a NameError on current_admin_user. For some reason, outside of the configuration blocks, the active admin current_admin_user helper doesn't exist.
So, how could I go about filtering actions based on the current user's priviliges?
you have to use a proc... current_admin_user works only when the app it's running, not when you declare your class..
example..
action_item :only => [:edit], :if => proc { current_admin_user.developer? } do
#enter code here
end
You can also use CanCan for this.. and place controller.authorize_resource at the beginning. Check the activeadmin documentation for this.
Another way to do it is overriding the action_methods in the ActiveAdmin controller.. like this
actions :all
controller do
def action_methods
if current_admin_user.developer?
super
else
super - ['show', 'destroy', 'new', 'create']
end
end
end
this works cool if you have multiple roles.
btw you should use developer? instead of developer (off course if the developer method returns a boolean as I suspect)
UPDATE
in version 0.6+ you should stick with CanCan, for more information check the activeadmin documentation http://www.activeadmin.info/docs/13-authorization-adapter.html#using_the_cancan_adapter