I am implementing an admin panel with Active Admin for my rails application.
I have a comment section inside admin panel, and I'd like to approve a group of comments by checking the comments I'd like to approve and then clicking update button.
In my previous version of admin panel part of page that did group updating looks like this:
<%= form_tag(:controller => "admin/comments", :action => "update_statuses") do %>
<table>
<% #comments.each do |comment| %>
<tr>
<td><%= check_box_tag("comments[]", comment.id) %></td>
#More table cells with info about comment
</tr>
<%end%>
</table>
<%= select_tag "status", options_for_select(comment_statuses) %>
<%= submit_tag "Update" %>
<% end %>
How to get similar functionality in active admin?
Right now active admin is missing such a feature. If you want to go for alternate rails admin interface then rails_admin is much better for such kind of functionality.
Batch functionality is currently being worked on and introduced in the master branch of ActiveAdmin. Heres how it looks.
http://dribbble.com/shots/265461-Active-Admin-Batch-edit-popover
Related
I'm using the public_activity gem to track changes with my Reposts model, which contains a comment_id and belongs_to the Comments table. In the _create.html.erb partial for the create action of a Repost, I can use this code with no errors:
<%= activity.trackable.comment %>
and the view will display this text:
Comment:0x00000004508ee0>
Which proves that it's displaying the Activerecord using the relationship I established between Reposts and Comments. However, once I try to extend the code to show the content field on the Comments table I get an error. For example:
<%= activity.trackable.comment.content %>
Returns the following error:
undefined method 'content' for nil:NilClass
activity.trackable.comment seems to pull the right record. How can I extend this so not only does it pull the record via the established relationship, but that it also pulls another field from that table?
Thanks!
EDIT 1: Full view for views/public_activity/_create.html.erb
<% if activity.trackable %>
<%= link_to activity.trackable.comment, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
EDIT 2: Per Leo's assistance, all I had to do was check for nil. Here's what I had to change my code to:
<% if activity.trackable && activity.trackable.comment.present? %>
<%= link_to activity.trackable.comment.content, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
I highly recommend adding that additional code to check for nil if you followed along the public_activity Railscast like I did.
So it seems per this particular question, the activity.trackable.comment was returning a NilClass object for some activities causing the exception to be thrown.
The fix of course is to check to make sure comment is not nil by doing the following
<% if activity.trackable && activity.trackable.comment.present? %>
<%= link_to activity.trackable.comment.content, activity.trackable.comment %>
<% else %>
which has since been removed
<% end %>
It seems like you're accessing the wrong object to get a count from. I've used the impressionist_count method for this sort of task in the past, the documentation here might help you.
I started rails a week ago, I want to know that how to display an unmodifiable text using erb file,
<%= f.label :email %>
<%= f.text_field :email %>
Here, instead of a text-box, I need a simple text displaying email id.
Any clues?
By "unmodifiable text" in a text field, I'm assuming you don't just want to have the email displayed on screen (if so, then Yuri Barbashov is correct) but also want it inside an input text field, but be unmodifiable.
In that case, you might be best off using javascript. Have a look at this StackOverflow answer for some JQuery examples.
Edit:
Actually, the solution here may be easier if you never plan on re-enabling the text field:
<%= f.text_field :email, disabled: true %>
<%= #your_model.email %>
But to be more precise you have to show your view code
I finally figured out how to implement pg_search's multisearch feature. But I'm having trouble making a usable search results page that displays links back to the various articles and faqs that contain the search terms. It's a pretty basic setup using Rails 3.2.3:
Models:
I have an Articles and a Faqs model, both with "Title" and "Content" attributes, and this code in both models:
include PgSearch
multisearchable :against => [:title, :content]
Search Form View Code:
The search form passes everything to a controller called "results."
<%= form_tag result_index_path, method: :get do %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "GO", name: nil %>
<% end %>
Results Controller:
class ResultController < ApplicationController
def index
#pg_search_documents = PgSearch.multisearch(params[:query])
end
end
I would like to make a search results page that displays the title of each result found, with a link back to that item. I figured out how to pull the 'class' attribute out #pg_search_documents. My thinking is to do something like this on the results page:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{pg_search_document.searchable.class}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
An example link that this code yields is: http://localhost/Article/3. If I could figure out how to downcase and pluralize "pg_search_document.searchable.class", I'd be home free. I've tried writing various methods in the model and controller, and tried writing a helper. But this is where my Rails skills fail me.
Any suggestions? Anybody know of a more elegant way of accomplishing this? Any ideas / suggestions are very much appreciated.
I did something similar and just used
<%= link_to pg_search_document.searchable.title, pg_search_document.searchable %>
to let Rails dynamically create the path to the associated record.
Well, it's amazing what walking away from the problem for a little while does. That, and more persistent Googling on basic Ruby. Here's the solution I came up with:
<ul>
<% #pg_search_documents.each do |pg_search_document| %>
<li><%= link_to pg_search_document.searchable.title, "../#{(pg_search_document.searchable.class).to_s.downcase!.pluralize}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>
Still, this seems ugly to me. I'd still be very interested to see something more streamlined and intelligent.
EDIT NOTE
I am rewording this question entirely now that I have a bit better understanding of rails & devise.
I am looking for a way to utilize a single table structure (Account) to create various account types.
What I am now having a hard time with is a structure where I need my Business to have an account but not necessarily vice versa (an Account could just be a typical user). I think the easiest approach would be just to have a 1 to 1 relation as opposed to inheritance but I could be mistaken there.
The reason its confusing to me is the registration process. If I accept the account information, I believe I could use accepts_nested_attributes_for to accept the account information but im afraid that'll break the workflow that devise is expecting. I considered overriding Devise::RegistrationController but I don't really know how rails is going to handle that (ie, if I call super but I am dealing with a Business rather than an Account - what happens?)
You can use CanCan to make account roles, and ask in your code current_user.role?(:admin)
There is good app template with device/cancan/spike integrated:
https://github.com/augusto/devise-cancan-spike
There is no problem per-se with having a form which manages multiple models, so long as the models are related to one another.
The 'stock' way of achieving this would be to use 'accepts_nested_attributes_for' in your model.
For your situation, you'd do something like this:
class Employee < ActiveRecord::Base
belongs_to :business
accepts_nested_attributes_for :business
end
Then in your registration view, you would use:
<!-- validation errors etc -->
<%= form_for #employee do |f| %>
<!-- all your employee fields etc -->
<%= f.fields_for :business do |b| %>
<p>
<%= b.label :name %>
<br/>
<%= b.text_field :name %>
</p>
<!-- more fields from business -->
<% end %>
<% end %>
If you wanted to handle both employee and 'normal user' registration in the same form, you could probably do something like this (never tried this, but I think it should work!):
<!-- validation errors etc -->
<%= form_for #person do |f| %>
<!-- all your person fields etc, assuming no extras for employee -->
<% if #person.respond_to? :business %>
<%= f.fields_for :business do |b| %>
<p>
<%= b.label :name %>
<br/>
<%= b.text_field :name %>
</p>
<!-- more fields from business -->
<% end %>
<% end %>
<% end %>
P.S. you mentioned in your question that you were worried Devise wouldn't cope with nested attributes. It definitely does, as I do exactly this in one of my applications.
I want to be able to show a datetime stamp ( I want it to appear like - "Posted 5 days ago" ), but i dont want the date field to appear when an user is making a post.
I just want to post to add it automatically. Any help? (Im just learning Rails so its all new to me...)
Thanks in advance.
For a Forum Application I did this:
<td class="right">
<% if forum.most_recent_post %><%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by <%= link_to forum.most_recent_post.user.login, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %> no posts <% end %>
</td>
Here <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by "username" so, you may try this. I think that can work in this scenario too. Just use distance_of_time_in_words_to_now post.time here post.time is the time when user made a post.