Rails ActiveAdmin: Incorrect breadcrumb - ruby-on-rails-3.1

I have model called Part which is using ActiveAdmin, CRUD operation works fine but the breadcrumb is not generating properly. Here is what I am getting in the breadcrumb on the EDIT page
Admin / Parts / #<Part:0xcd74ef0> /
I am using "activeadmin", "0.5.0"

In Active Admin for Custom breadcrumb
Try to define display_name in your Part model.
class Part < ActiveRecord::Base
// Some Code
def display_name
"#{ id } #{ name }"
end
end

Try this.. this might help someone..
ActiveAdmin.register Post do
breadcrumb do
[
link_to('Admin', admin_root_path),
link_to('My Resource', admin_<resource>_path),
]
end
end

You can use to_s method.
Define to_s method in Part model.
class Part < ActiveRecord::Base
def to_s
attribute_name.to_s
end
end

Related

Get current controller and action name in Ramaze

Is there a way to get the currently executing controller and action name in Ramaze?
require 'ramaze'
class FooController < Ramaze::Controller
def bar
"#{controller}.#{action}" #how to get this to work?
end
end
PS: I know self.class.name would give you the class name/controller name but was wondering if this captured somewhere else in the Ramaze API. Also __method__ should give the action name but I don't want to create variables when I pass this info to templates.
You can get them with :
controller = action.node
method = action.method
See https://github.com/Ramaze/ramaze/wiki/Knowing-which-controller-or-method-triggered-view-layout-rendering

NameError when assigning instance of model to reference property

I'm unable to assign a model instance to a reference property of another model. Relevant code is below:
module Blog::Models
class Post < Base; belongs_to :user, dependent: :destroy end
class User < Base; has_many :posts end
...
class BasicFields < V 1.0
def self.up
create_table User.table_name do |t|
...
end
create_table Post.table_name do |t|
...
t.references :user
end
end
...
end
end
module Blog::Controllers
...
class PostEditN
...
def post(post_num)
#post = Post.find(post_num)
#user = User.find(#input.user)
...
#post.user = #user # Error thrown: NameError at /post/edit/1 uninitialized constant User
# #post.user_id = #user.id << This is my currently working solution
#post.save
redirect PostN, post_num
end
end
...
end
...
When I assign something to #post.user using Camping in console mode, it is successful, but I can't seem to accomplish the same behavior in the controller otherwise. I made do by simply assigning the #user.id to the user_id property of the Post instance. However, I would like to figure out why the alternate method works in the Camping console and not when I'm simply running the webserver.
My best guess is that this is a problem with namespaces. In the code you show Useris actually Blog::Models::User. In your controller the context is Blog::Controllers. Have you tried changing the code in the controller to?
#post = Blog::Models::Post.find(post_num)
#user = Blog::Models::User.find(#input.user)
...
I was able to resolve my issue. Seems when I was creating new Post records, I was not initializing the User. Thus, when assigning #post.user it would complain that the user property was uninitialized. The only problem I see is that an operation was attempted to be made on an oprhan Post record, which is invalid data according to the relationship with User.

Admin panel rails 3

I want to make an admin controller, it has following 3 views index, members(list of users) and commet(all the feedback from the user) for the following i made following actions in controllers
class AdminController < ApplicationController
before_filter :authenticate_user!
def index
#members = User.all
#comment = Feedback.all
end
def member
#members = User.all
end
def comment
#comment = Feedback.all
end
end
Now I want to make comment and member view such that the admin can delete or edit the feedback or user, now how should write the methods for that?, since all this is happening in the same page?
I did not know what to search for this problem. So I am posting it, it might be repeated

Delegating to another action in Ramaze

I'd like to delegate to the Show action from the Index action if an id was passed. I can't seem to get it to work, what am I doing wrong here?
require 'ramaze'
require 'slim'
class UsersController < Ramaze::Controller
engine :slim
def index(id=nil)
if id
render_full "/users/show/#{id}" #id was passed, "show" the item
end
#alright just render the "index.slim" here...
end
def show(id)
u=User[id] #ORM stuff...
end
end
Ramaze.start
You have to call return render_full(...) opposed to just render_full(), without this the code below it will be executed regardless of whether or not an ID was specified.
You can call return show(id) instead of render_full "/users/show/#{id}"

How can I test the setting of a composed object's property using RSpec?

Say for example I have two classes within my Rails application - Customer class and a Card class. The Customer class is composed of a Card class i.e. the customer has a card.
I then have a Rails controller with a 'do_something' action defined, which will initialise a new instance of Customer (which in-turn will internally create a new instance of Card) using the params passed in on the POST.
The number of the card is then set as follows:
class ShopController < ApplicationController
def do_something
customer = Customer.new params
customer.card.number = params[:card_number]
...
end
end
How is this assignment of the card number tested in an RSpec test? Ideally, if 'should_receive_chain' existed we could write:
describe MyController do
describe "POST 'do_something'" do
it "should set card number"
params = { :card_number => '1234' }
card_mock.should_receive_chain(:card, :number).with '1234'
post :do_something
end
end
end
Any ideas? Perhaps the fact that it can't be tested easily is a code smell, and maybe I should create a setter method on the Customer class?
I do think you are testing this at the wrong level. If you are wed to setting the card number in a separate statement then it might be better to create a function to help with this.
class Customer
def self.new_with_card_number(params, number)
customer = new(params)
customer.card.number = number
customer
end
end
describe Customer do
it 'creates a card with a number' do
customer = described_class.new_with_card_number({}, '1234')
customer.card.number.should == '1234'
end
end
You could then change your controller to:
class ShopController < ApplicationController
def do_something
customer = Customer.new_with_card_number(params, params[:card_number])
end
end
A simpler solution might be to simply name the parameter in your form so that setting the card number is done automatically:
params[:customer][:card_attributes][:card_number]
You could then just change the call to customer = Customer.new(params).
I would change the Customer model to add accepted_nested_attributes_for :card and change the controller action to
class ShopController < ApplicationController
def do_something
customer = Customer.create params[:customer]
...
end
end
then your spec can look like
describe ShopController do
describe 'POST /do_something' do
it "sets the card's card number" do
post :do_something, :customer =>
{
:card_attributes => {:card_number => '1234'}
}
Customer.last.card.number.should == '1234'
end
end
end

Resources