Project

General

Profile

Plugin tutorial - Pools from project menu - error after pressing Yes/No

Added by Massimo Barbera about 12 years 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 (15)

RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Harry Garrood about 12 years 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 about 12 years 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 about 12 years 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 about 12 years 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 about 12 years 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 about 12 years 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 about 12 years 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'
with
redirect_to :action 'index', :project_id => @project.id

RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Massimo Barbera about 12 years 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 about 12 years 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 about 12 years 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 about 12 years 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, Herve Harster is nil here as well)

Look on http://guides.rubyonrails.org/caching_with_rails.html and go down to 1.5 SQL caching - that's what is happening here. Your cached query (Project.find...) is being destroyed at the end of each action. Your two options (at least, the two I can think of) are:
  1. use some other kind of caching (see that webpage, I don't know about it)
  2. 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 almost 12 years ago

I had the same problem. My solution was this line of code.

before_filter :find_project, :authorize, :only => [:index, :vote]

RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Stanley Nguyen over 10 years ago

hi,

has anyone come up with a solution for this? I think the main problem is project_id is nill. It works fine if I hard-code the value.

def find_project
  # @project variable must be set before calling the authorize filter
logger.info (@project.nil? ? "@project is nil" : "@project = #{@project}")
@project ||= Project.find(params[:project_id])
#@project = Project.find('my_project')
end

Thanks

RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Tim Whelan about 10 years ago

Hi

I'm new to Ruby even without Rails and Redmine but I decided to jump in the deep end and see how far I can get - take my help with a pinch of salt...

Environment:
Ruby 1.8.7
Rails 3.2.12
Redmine 2.2.3

I'm following the plugin tutorial and I encountered the primary problem described above ('Couldn't find Project without an ID'). I got around it by changing the index.html.erb file to pass the project identifier in the POST method as a project_id parameter when voting. I hard-coded the project_id param passed in the POST method into the index.html.erb file so if I added the Polls plug-in to a different project I think it'd only ever vote on the project identified by the hard-coded project_id in index.html.erb.

I think this is basically the second workaround that Harry Garrood suggested. My implementation needs to be a little more flexible in that the project identifier probably shouldn't be hard-coded in the index.html.erb file but obtained pragmatically. I'm not familiar enough with Ruby/Rails/Redmine to know how to do that but hopefully this helps a bit.

I've attached my Polls_controller.rb and index.html.erb files with permissions code commented out - everything seems to work. Hopefully they make sense.
________________________________________________________________________

On the permissions side of things; I ran into the same problem you described - 404 when trying to vote. My symptoms were slightly different though.

  • I had one Manager member that could view the polls but not vote
  • I had one Developer member that could only vote but not view the polls
  • If the manager tried to vote on the first poll a 403 - Access Forbidden - was returned. This makes sense because the manager doesn't have permission to vote.
  • If the manager tried to vote on the second poll a 404 was returned.
  • If the developer tried to vote on any of the polls I think a 404 was returned.

I un/commented the permissions code a few times while investigating the above issues and now the permissions stuff is working too. This doesn't help your issues with the permissions, Massimo, especially since I don't know what happened to resolve this. However, it does at least give reassurance that the permissions portion of the tutorial isn't broken.

I originally thought it might be that the project_id was being substituted for the poll's id. Since there was one project and two polls that would explain why only the first poll behaved as expected when the manager tried to vote. However, thinking back, it may be that I originally had the end of the PollsController definition located before the 'private' keyword. I remember noticing that and changing it. I've added in a comment for this so you can see where I mean. Maybe check that you didn't do the same thing...
________________________________________________________________________

Summary:
  • I hard-coded the project identifier in the index.html.erb file to pass the project_id param when voting. This allowed voting to take place.
  • I don't know how I solved the permissions/404 problem but I think it was the misplaced end keyword in polls_controller.rb.

This thread seems old but I hope this helps someone. I'm going to need much good forum karma in the future.

RE: Plugin tutorial - Pools from project menu - error after pressing Yes/No - Added by Tam M over 5 years ago

Here are the files that worked for me (attached).

I used Tim's changes (good karma to you!) with a few differences:

1. I kept the before_filter in.
2. The index method uses Poll.all
3. It was not necessary to hard code a project id.
4. My plugin was called "tutorial_polls" so that is what is at the end of index.html.erb for the stylesheet.

Environment:
Redmine 3.4.5
Ruby 2.3.3p222
Rails 4.2.8

    (1-15/15)