access controller's object attributes in view page in ROR - ruby-on-rails

I am new to ROR and learning it. In my controller I have an admins
record and I am passing that admin object to the admin's view page to
get the name of the admin. But when I try to access the name it
showing a error:
undefined method 'name' for :current_admin:Symbol
Please help..
Please find my code below
Sessions Controller
def create
admin=Admin.find_by_email(params[:session][:email].downcase)
if admin && admin.authenticate(params[:session][:password])
redirect_to(admins_index_path(:current_admin=>admin))
end
end
In view page of index_admin

In your action for index_admin you need to get an admin object from your param. The value of a param is usually just a string.
def create
admin=Admin.find_by_email(params[:session][:email].downcase)
if admin && admin.authenticate(params[:session][:password])
redirect_to(admins_index_path(:current_admin => admin.id)) # pass the id, not the object
end
end
def index_admin
#current_admin = Admin.find(params[:current_admin])
end
In you index_admin.html.erb:
Hi <%= #current_admin.name %>

Related

Trying to access current user in rails admin

Is there a way to get access to the current_user in rails admin config.model filterable option. I have tried using:
bindings[:view]._current_user
but bindings is nil for me
Here's a code snippet of what I've been doing:
field :user do
filterable do #I want to access current user here
bindings[:view]._current_user #bindings is nil
end
pretty_value do
bindings[:view]._current_user #bindings is not nil
end
end
I couldn't access it via the bindings approach so I just made a before filter in my application controller that calls a method there which sets the current user in my User model via a class method.
Thus, in my code snippet, I could access current_user like so:
...
filterable do
User.current
end
...
Answer came from this:
https://amitrmohanty.wordpress.com/2014/01/20/how-to-get-current_user-in-model-and-observer-rails/
I was able to use:
field :user_id do
default_value do
bindings[:controller].current_user
end
end

How should I display download links to only certain users in ActiveAdmin rails?

I want to hide download links according to user role of logged in user in Active Admin
I want to be doing it like this but I'm not able access current_user
index :download_links => current_user.admin? do
# columns
end
Your question seems to be incomplete.
Guess- You have a User model, id, role, etc as attributes
First, Find the user first based on id, #current_user = User.find(params[:id)
Then,
if #current_user.admin?
# do not show the links
end
Hope you need this to use in views, so use along <% rails tag %>
Try to use current_admin_user, b/c the activeadmin user is called `AdminUser
Devise helper
Or u can define your own method in active_admin.rb like:
config.current_user_method = :current_user

rails pass variable to model from controller not working

Im trying to pass a variable from controller to my form to no avail.
Its working in another controller but cannot make it work for new user registrations.
def update
#profile = Profile.find(params[:id])
#profile.form = "signup"
...
end
Inside model I can do #profile.form to get the value
This does not work for new user registrations:
inside create action:
undefined method `form=' for nil:NilClass
how to pass a variable to my model upon new user registration? User = User.new then #user.form == "signup" does not work either, i have :form in my attributes accessible list
Part of model:
attr_accessible form
...
validates :city_id, :presence => true, :if => :signup?
def signup?
##profile.form == "signup"
##user.form == "signup"
if self.form == "signup"
return true
else
return false
end
end
EDIT 1:
Still unable to pass a param to the model :S:S:S tried every possible way and google found solution.
The create method for my registrations#create =
def create
profile = Profile.new
profile.form = "signup"
super
end
I am not sure what exactly your problem is .
What I understand after I read your problem statement twice is that you tried to "new" a instance of Profile, assign its form attribute. And use its instance method signup? to do validation. But you get an error after on self.form because self reference to the nil Class
I think you should change profile to #profile inside the create method.
Just by guess after reading ruby guide about instance variable,
My thought is that declaring the instance variable that will be added dynamically inside the controller structure and later they are passed to the model. And it is always #... inside controller generated by rails g scaffold.
Just give it a try :)
Interesting. I believe what you want to do is have a virtual attribute 'form' of an object a certain value. I.e. does form exist in the database as well. If it doesn't you should be using attr_accessor :form
Documentation
This defines the getter and setter method for form in the class where it is being invoked.
However, the error you stated is something radically different.
undefined method `form=' for nil:NilClass
This merely means a profile with the id being passed in the params does not exist. i.e. if we are updating params id = 222, a database record with id 222 does not exist for Profile.

How do I respect RESTful methods when using find_or_initialize_by in Rails 3.2?

I have a resource in my project that collects some information from a user. Basically it's a form that they fill out before they can access another area of the site. It then sets a cookie for a week, but if they come back it will look up their previous entry and keep their preferences tied to them (and will update any details as long as the email address matches).
Currently I have a Applicants controller that looks like this:
class ApplicantsController < ApplicationController
...
def create
#applicant = Applicant.find_or_initialize_by_email(params[:applicant])
if #applicant.new_record? ? #applicant.save : #applicant.update_attributes(params[:applicant])
set_cookie_and_redirect
else
render 'new'
end
end
def update
if #applicant.update_attributes(params[:applicant])
set_cookie_and_redirect
else
render 'new'
end
end
end
The set_cookie_and_redirect is a private method that just sets some cookies and redirects the user to a page. The code works, but it just feels dirty. It's essentially updating a record within the create method under the condition that it's not a new record. I'm also forced to have an update method in case an existing record comes back with a validation error--the form helper will then switch the form over to sending to the update method.
So to my point... is there a more appropriate way to push the update_attributes call in the create method to the update method? Or better put, is there a better way to respect the RESTful methods in isolating the create and update functionality?
UPDATE: I wanted to be a little more specific too. If the user has filled this form out before it will set a cookie so they don't have to fill it out again for seven days. However after seven days the cookie is expired and they see the form again. The controller doesn't know if the user is new or existing until they add user input into the form which is then compared based on the email address.
Thanks in advance! I definitely look forward to anyone's thoughts on this.
The create method should only create, and the update method should only update. Let Rails decide which is going to happen based on what is inside of #applicant when the form is rendered - It essentially does what you're doing: Checks if the record is new or not, and sends it to update/create accordingly. Example:
def applicant
#applicant = Applicant.find_or_initialize_by_email(cookies[:email])
# renders applicant.html.erb form
end
<%= form_for #applicant do |f| %>
# ... fields ...
<% end %>
def create
#applicant = Applicant.new(params[:applicant])
#applicant.save
# .. etc.
end
def update
#applicant = Applicant.find_by_email(cookies[:email])
#applicant.update_attributes(params[:applicant])
# ... etc.
end
Rails will send the request to the correct action based on the new_record? status of the Applicant object.

Rails 3 create default nested objects

I have a form where the user signs up and creates an Account, an User and a Website.
def new
#account = Account.new
#account.users.build
#account.websites.build
...
end
def create
#account = Account.new(params[:account])
...
Everything works fine. Now, I want to create a default Page with Page.title = "homepage" and Page.body = "".
How can I do that? I tried different options and it doesn't work. For example, I do this #account.websites.pages.build and I get this undefined method pages for []:ActiveRecord::Relation.
The collection returned by #account.websites is an array, rails can't intuit which member of the collection you're trying to create an associated object on... You need to specify which website you want to build a page for, ie
#account.websites.first.pages.build

Resources