Rieles: la identificación no se pasa del menú desplegable al enlace

What I'm trying to achieve is to have a list of projects in a dropdown menu, so that when one is selected from that dropdown, only tasks etc relevant to that project are shown on other pages. I got it working fine by having the button in that project's show view, but it would be a lot quicker if the user could select it this way round.

The below code shows what I've got so far in the index view (thanks to cutalion as well!) - the line that seems to be the problem is the third one - the select link. The error it's giving is that it can't find a project without an id. There is a project listed in the dropdown when the page displays (when I remove that line) - so why is the id not getting passed? Additionally, when the third line is removed from the code to get the page to display, the link generated by the second line points to the index view rather than the show view. I'm guessing this is all to do with the id not getting passed, but I have no clue how to fix it!

Any light anyone could shed on this would be great, thanks!

projects/index view (code updated to correct inconsistancies):

<%= collection_select(:project, :id, Project.all, :id, :name) %>
<%= link_to 'Show', @project, :id => "show_project_button" %>
<%= link_to "Select" select_project_project_path(@project), id: "select_project_button" %>


<script type="javascript">
  $('#project_id option').change(function(){
    var project_id = $(this).val();
    $('#show_project_button').attr('href', '/projects/' + project_id);
    $('#select_project_button').attr('href', '/projects/' + project_id + '/select_project');
  })
</script>

Also, the select_project action in the projects controller:

def select_project
  project = Project.find(params[:id])
  session[:project_id] = project.id
  redirect_to root_url, notice: "Current project set to: #{project.name}, ID: #{project.id}"
end

preguntado el 04 de julio de 12 a las 09:07

2 Respuestas

It seems that @project is empty, therefore it can't find any id.

Change this line in the controller to:

@project = Project.find(params[:id])

You also have to consider what happens if the project can't be found. If this happens, you will get the same problem as now.

Respondido 04 Jul 12, 10:07

Thanks, I've changed the controller code, and it still is coming up with the error 'couldn't find project without an ID'. As for what happens if the project can't be found, the dropdown should only be populated with projects that are in the db - so would that happen? If so, I guess I could do some sort of rescue? - ecs

Just check your project variable by adding <% raise @project.inspect %> in the first line of your view. Do you get all projects from the db in the index controller and store it to @project? - Daniel Blaichinger

I don't know if you posted your actual code, but there's a mismatch on the ids there:

<%= collection_select(:project, :id, Project.all, :id, :name) %>
<%= link_to "Show", @project, id: "show_project_btn" %> # this should be id: "show_project_button"
<%= link_to "Select" select_project_project_path(@project), id: "select_project_btn" %> # and this should be id: "select_project_button", right?

<script type="javascript">
  $('#project_id').change(function(){
    var project_id = $(this).val();
    $('#show_project_button').attr('href', '/project/' + project_id);
    $('#select_project_button').attr('href', '/projects/' + project_id + '/select_project');
  })
</script>

also, you have inconsistent paths in javascript

$('#show_project_button').attr('href', '/project/' + project_id); // rails generates /projects, I think

Respondido 04 Jul 12, 10:07

You're quite right there - that was code that I came up with with someone else's help - I didn't check it properly!! I have corrected the inconsistancies but am still getting the error 'can't find project without an ID' - any clue why? I've changed my controller code as suggested by @Daniel as well. - ecs

I can only provide you with some clues, since I cannot reproduce the situation: - Have a look at the path that gets appended to the select_project_button (does it have an id) -console.log($(this).val()) to see if this actually passes an id on change of the select - Vence a Richartz

Ok - what I want to know then is - does the dropdown look like it should be supplying the id from the code that is there? If so, then maybe my method for extracting the id from it is not correct? Maybe that's the part I need to look in to? If the code looks like it will never work as it can't supply the id, then I'd have to look into another way of doing it. Thanks! - ecs

I would say it looks correct, but you actually have to find out if it gets triggered (maybe #project_id is the wrong id?) and if it passes the right values to the buttons - Vence a Richartz

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.