Plugin tutorial - Pools from project menu - error after pressing Yes/No
Added by Massimo Barbera over 1 year ago
Dear all,
this thread has been opened as a continuation of thread "Possible error in plugin tutorial?" (http://www.redmine.org/boards/3/topics/28026?r=28323)
as the original issue has changed but the referring item is the same.
So summarizing:
- working with Redmine to 1.3.0
- followed Plugin Tutorial (http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial)
- everithying works fine until I perform the following change in the controller:
def index 2 @project = Project.find(params[:project_id]) ...
- after that I have a problem when I click 'yes' or 'no' in the poll as I got the following error:
ActiveRecord::RecordNotFound in PollsController#index
Couldn't find Project without an ID
And... clicking the "BACK" button of the browser the yes/no counter is correctly updated
Does anyone has a suggestion?
Thanks
Replies (12)
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
If you keep going in the tutorial up to this bit:
class PollsController < ApplicationController
unloadable
before_filter :find_project, :authorize, :only => :index
[...]
def index
@polls = Poll.find(:all) # @project.polls
end
[...]
private
def find_project
# @project variable must be set before calling the authorize filter
@project = Project.find(params[:project_id])
end
end
does it work?
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera over 1 year ago
Hi,
if I keep going in the tutorial, permissions work fine but I keep on having the same issue after selecting "yes/no"
ActiveRecord::RecordNotFound in PollsController#index
Couldn't find Project without an ID
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
Not sure if this is the correct solution, but maybe try:
def find_project
@project ||= Project.find(params:project_id])
end
which I think should then only try to find the project from params[:project_id] if @project has not been set already.
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera over 1 year ago
Harry, thanks for the suggestion.
I have tried and unfortunately I still have the same result.
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
Okay, try this and tell me what you see in the log:
def find_project
logger.info (@project.nil? ? "@project is nil" : "@project = #{@project}")
@project ||= Project.find(params:project_id])
end
After changing this, load the index page and then vote, and then paste the log for those two requests.
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera over 1 year ago
Hi I think there is something weird going on here... sorry if this might be confusing...
I am running with administrator rights and after selecting "Polls" in the project menu I got "404
The page you were trying to access doesn't exist or has been removed". I am pretty sure iI did not change anything since it was working on Thursday
The controller that led to this result is the following (I have inserted the log line you suggested but the behavior is the same without it):
class PollsController < ApplicationController
unloadable
before_filter :find_project, :authorize, :only => :index
def index
@polls = Poll.find(:all) # @project.polls
end
def vote
poll = Poll.find(params[:id])
poll.vote(params[:answer])
if poll.save
flash[:notice] = 'Vote saved.'
redirect_to :action => 'index'
end
private
def find_project
logger.info (@project.nil? ? "@project is nil" : "@project = #{@project}")
@project ||= Project.find(params[:project_id])
end
end
end
So I have tried to comment out authorization stuff, with the following controller:
class PollsController < ApplicationController
unloadable
# before_filter :find_project, :authorize, :only => :index
def index
@polls = Poll.find(:all) # @project.polls
logger.info (@project.nil? ? "@project is nil" : "@project = #{@project}")
@project ||= Project.find(params[:project_id])
end
def vote
poll = Poll.find(params[:id])
poll.vote(params[:answer])
if poll.save
flash[:notice] = 'Vote saved.'
redirect_to :action => 'index'
end
# private
# def find_project
# logger.info (@project.nil? ? "@project is nil" : "@project = #{@project}")
# @project ||= Project.find(params[:project_id])
# end
end
end
and one selecting "Yes/no" I am brough to the well known "Couldn't find Project without an ID". The following comes from production log:
Processing PollsController#vote (for 127.0.0.1 at 2012-01-09 18:51:18) [POST]
Parameters: {"action"=>"vote", "authenticity_token"=>"oeC+3SKxsRe5bBkXmERp+TyA35aFfSq6iwT0Pxpx9/g=", "id"=>"1", "controller"=>"polls", "answer"=>"yes"}
Redirected to http://127.0.0.1:8080/redmine/polls
Completed in 16ms (DB: 0) | 302 Found [http://127.0.0.1/redmine/polls/vote/1?answer=yes]
Processing PollsController#index (for 127.0.0.1 at 2012-01-09 18:51:18) [GET]
Parameters: {"action"=>"index", "controller"=>"polls"}
@project is nil
ActiveRecord::RecordNotFound (Couldn't find Project without an ID):
app/models/project.rb:250:in `find'
vendor/plugins/redmine_polls/app/controllers/polls_controller.rb:9:in `index'
config/initializers/mongrel_cluster_with_rails_211_fix.rb:62:in `dispatch_cgi'
Rendering rescues/layout (not_found)
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
The logger line shouldn't make any difference to the behaviour, it was just to try to work out what was going on.
I'm not sure if this is the right way to do it, but try replacing
redirect_to :action 'index'withredirect_to :action 'index', :project_id => @project.idRE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera over 1 year ago
Right Harry,
I have inserted the log line you suggested (logger.info...) but the behavior is the same without it.
Talking about the new suggestion, I have changed my line to
redirect_to :action 'index', :project_id => @project.id
and when I select "Polls" tab in the project menu I have the following error:
SyntaxError in PollsController#index
C:/Program Files/BitNami Redmine Stack/apps/redmine/vendor/plugins/redmine_polls/app/controllers/polls_controller.rb:17: syntax error, unexpected tSTRING_BEG, expecting kEND
redirect_to :action 'index', :project_id => @projec...
^
C:/Program Files/BitNami Redmine Stack/apps/redmine/vendor/plugins/redmine_polls/app/controllers/polls_controller.rb:17: syntax error, unexpected ',', expecting kEND
... redirect_to :action 'index', :project_id => @project.id ...
from the log
Processing ProjectsController#show (for 127.0.0.1 at 2012-01-10 13:57:35) [GET]
Parameters: {"action"=>"show", "id"=>"prova1", "controller"=>"projects"}
Rendering template within layouts/base
Rendering projects/show
Completed in 156ms (View: 62, DB: 62) | 200 OK [http://127.0.0.1/redmine/projects/prova1]
Processing ApplicationController#index (for 127.0.0.1 at 2012-01-10 13:58:05) [GET]
Parameters: {"project_id"=>"prova1", "action"=>"index", "controller"=>"polls"}
SyntaxError (C:/Program Files/BitNami Redmine Stack/apps/redmine/vendor/plugins/redmine_polls/app/controllers/polls_controller.rb:17: syntax error, unexpected tSTRING_BEG, expecting kEND
redirect_to :action 'index', :project_id => @projec...
^
C:/Program Files/BitNami Redmine Stack/apps/redmine/vendor/plugins/redmine_polls/app/controllers/polls_controller.rb:17: syntax error, unexpected ',', expecting kEND
... redirect_to :action 'index', :project_id => @project.id ...
^):
config/initializers/mongrel_cluster_with_rails_211_fix.rb:62:in `dispatch_cgi'
Rendering rescues/layout (internal_server_error)
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
My fault, should be
redirect_to :action => 'index', :project_id => @project.id
By the way, if things do go wrong (especially syntax errors), your first port of call should be http://api.rubyonrails.org, assuming you are on Rails 3.1.
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera over 1 year ago
Thanks Harry, I have tried it and now the error message has slightly changed:
Couldn't find Project with ID=4
instead of the previous
Couldn't find Project without an ID
Talking about Rails version, I am using Bitnami stack Version: 1.3.0-1.
I guess I can check Rails version looking in its changelog.txt, where I can see "* Update Rails to 2.3.14"
Do you think this fault might be related to Rails version?
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood over 1 year ago
No, I don't think it's related to the Rails version - not if the rest of your Redmine works properly. That is a list of what Bitnami have done to their stack recently, so that means your stack will have Rails 2.3.14 (it's not an instruction)
You can check your rails version by running use_redmine.bat and typing rails --version
What appears to have happened here is that when you redirect to index, a parameter "project_id" is set with value "4" - which is the value of @project.id at that time.
4 is appearing because it's the value returned by 'id' for the nil object in ruby (ie, @project is nil here as well)
- use some other kind of caching (see that webpage, I don't know about it)
- pass project id as a parameter between all the actions.
Option two, perhaps easier, would mean changing your view to set the param project_id on the vote action, and then use the param to find the project. It may be worth enabling your before_filter again to help with keeping it DRY.
RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Denny Schäfer about 1 year ago
I had the same problem. My solution was this line of code.
before_filter :find_project, :authorize, :only => [:index, :vote]
(1-12/12)