active admin destroy action - ruby-on-rails

im working in a project writen in ruby on rails and im currently using the active admin gem for its content manager system of my site im just wondering how active admin is using the delete action im trying to overide it but i my code doesent work, i think we are having a problem in how to get the specific line to be destroy
def destroy
#menu = Menu.find(params[:menu_recipe][:menu_id])
#menu_recipe = #menu.menu_recipes.find(params[:id])
#menu_recipe.remove_recipe
#menu_recipe.destroy
redirect_to #reservation, :notice => "recipe destroyed"
end
it comes with a error of
undefined method `[]' for nil:NilClass

Probably params[:menu_recipe] is not set. Try putting params[:menu_recipe] ||= {} at the beginning.
But why is it that you're finding through the Menu, anyway? Can't you just do 'MenuRecipe.find(params[:id])'?

Related

Rails 4.1 can't do the #user_friendship.save in the controller and returns an error

Hi apparently cant save my work in rails 4.1 after adding the state machine gem and doing some more details like when you add someone in the friends i get an error in my controller the #user_friendship.save
this is the controller line
i got this error in my User_Friendships Controller and the line that contains the #user_friendship.save fails every time
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
#friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
#user_friendship = current_user.user_friendships.new(friend: #friend)
#user_friendship.save
flash[:success] = "You are now friends with #{#friend.full_name}"
redirect_to profile_path(#friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
this is the error that occurs when i add to the user friendship
NoMethodError in UserFriendshipsController#create
protected method `around_validation' called for #<StateMachine::Machine:0x007fe631d43a68>
{"utf8"=>"✓",
"authenticity_token"=>"gPaCH/833buhbi53USaprtbmvFGjNCHE7RYY+DW071s=",
"user_friendship"=>{"friend_id"=>"popo"},
"commit"=>"Yes,
Add friend"}
i am new in rails so can you please explain more so that i can undestand why this is happening and thank!
Apply this patch, Till they fix it, it will work
config/initializers/state_machine_patch.rb
module StateMachine
module Integrations
module ActiveModel
public :around_validation
end
end
end
Do you use the newest version of statemachine? If no, try the newest version of state machine.
https://github.com/pluginaweek/state_machine/issues/295

Can I override just part of an ActiveAdmin controller action using `super` + custom redirect?

I've registered a Widget in ActiveAdmin and want to change the redirect that takes place after creating a new one. So that I can accomplish various things with Javascript, I've created a custom form for creating/editing them such that in /admin/widget.rb I have this:
form do |f|
render "create_or_edit_widget"
end
I want to modify the basic Admin::WidgetsController#create action to change where the user is redirected after successfully creating one. I can fill out the rest of the custom action to complete this, except I don't know how to handle a case where the .save fails and the user is redirected back to the form with the formtastic inline error messages. I know how I could do this if I wanted the normal Rails form behavior of creating a list of error messages but not enough about Formtastic to copy its behavior. So far I have this:
controller do
def create
#widget = Widget.new(params[:widget])
if #widget.save
redirect_to admin_widgets_path, notice: "Successfully created Widget."
else
redirect_to :back
end
end
end
I was wondering if I can somehow user super and then only change the redirect path after successful creation instead of having to write out the entire action. If that's not possible, can anyone tell me where in the ActiveAdmin GitHub I'd be able to find the standard #create action so I can copy it out and change the one part?
Yes, you can do that. Here is a working code from my application using super and just changing the redirection
def create
super do |format|
redirect_to admin_submission_discussion_path(id: resource.discussion.slug, submission_id: resource.discussion.client_application.slug) and return if resource.valid?
end
end

Ruby on Rails Undefined method find_by_email

I have a weird issue, I can't seem to find_by_email working in my project, here's the part of my controller where I'm getting the exception. I am using rails 4.0.3
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Welcome back!"
else
flash.now.alert = "Email or password is invalid"
render "new"
end
end
I can't stop anything wrong with the code but still getting Action Controller: Exception caught:
NoMethodError in SessionsController#create
app/controllers/sessions_controller.rb:6:in `create'
undefined method `find_by_email' for #<Class:0x007fbdd62179b0>
I'm wondering if there was another way I could troubleshoot this? I've tried using the method manually in the rails console but receive the same issue.
As suggested by many, this is due to missing email column in users table.
If you have not run migrations yet, please do it.
Add email field in users table
Rails 4.1.0 will depreciate custom find by method
Use
User.find_by email: params[:email]
Sooner or later you have to change it.
reference
http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by

Create statement is not workin in rails

So I have a relationship of project to categories, such that a project can have many categories and a category belongs to a project. I have managed to have it working, but now - when I restartded my rails server, it doesn't work. The code I show bellow is what I had before the restarted and after the restart of my rails server, so I think its something to do with the code...
So I am not sure if you need models or just the controller or the form or what, so I have posted the create method from the categories controller as thats where the issue is happening:
def create
#project = Project.find(params[:project_id])
#category = #project.categories.create(params[:category].merge(:user_id => current_user.id))
if #category.save
redirect_to project_tasks_path(#project.id), :flash => {:success => 'Created a Category! Now you can create tasks!'}
else
redirect_to :back, :flash => {:error => 'We could not create an category. You need to enter a name.'}
end
end
It happens on the psychical create line, throwing the error:
RuntimeError in CategoriesController#create
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Rails.root: /home/adam/Documents/Aptana Studio 3 Workspace/StartPoint
Application Trace | Framework Trace | Full Trace
app/controllers/categories_controller.rb:14:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"qbJyilRiMtwOyPDq9HQFO4JME+TPkh/cCEEqPZPxGDw=",
"category"=>{"category"=>"ffffffff"},
"commit"=>"Create Category",
"project_id"=>"2"}
This is thrown upon hitting create. Any ideas or do I need to show more code?
It looks like you don't have a current_user so it is set to nil.
Calling .id on nil will result in this error in Rails.
If your application does not allow anonymous access then you have a problem.
If it does, you should do this:
current_user ||= User.new
To always init current_user to a new User instance.
Your code will still not work as User.new will create a unsaved instance.

Current_user nil after creating a session in ROR with AuthLogic

I'm having a bit of problems with AuthLogic and current_user.
I have a Flex4 application using the Cairngorm framework as the front-end, and Ruby On Rails as the back-end.
I can log in fine through a browser, and when only using ROR. However, when I try it through my Flex4 application, it will fail the first time but work the second time.
What is happening, is inside the user_sessions_controller.rb I have a call to
self.current_user.to_xml;
The first time I call the create action, the current_user object returns nil. The second time I call it (without restarting the server, or browser) it will be the proper current user.
So this leads me to believe that the current_user is being set sometime after the render command inside my create action.
If I need my rails controller to return the current user in the create action, how would I go about doing that?
Was just having the exact same problem...not sure why this happens but I was trying to call current_user in my create method in my user_sessions_controller.rb, solved as per below...
def create
#user_session = UserSession.new(params[:user_session])
if #user_session.save
current_user = UserSession.find.user
current_user.increment_login_count_for_current_memberships!
flash[:notice] = 'Sign in successful.'
redirect_to root_path
else
render action: "new"
end
end
Hope this helps!
This is because the helper methods current_user is typically defined as a before_action. What means, the before action did not run before you use it during the session create.
I also used this UserSession.find.user which is perfectly fine for this usecase.
Cheers,
Niklas

Resources