There must be an easy answer to this question, but I’m still unable to get it working.
I have a Ruby On Rails app that allows the user to manage projects with a database (create, update, show, edit, destroy, …, etc.). For example, when the user edits a projects, she/he is taken to a page, like http://:3000/projects/3/edit where she/he can modify entries the the form there. Notice the user is editing project 3 in this example.
To edit a project, the user click this button
<td><%= button_to 'Edit', { :action => "edit", :id => project }, :method => :get %></td>
which call this method
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end
and that works fine.
However, I added another method call updateFields, so that when a field is modified, a javascript function called updateFields is run and calls an AJAX request that modifies other related fields in the form. The following is an excerpt from updateFields:
<script language="JavaScript" type="text/javascript">
function updateFields(){
$.ajax({
url: "<%= update_fields_projects_url %>",
data: {
project_year: $('.project_year_class').val(),
project_type: $('.project_type_class').val(),
<snip>
}
});
}
</script>
In the controller, I can then get all the info I need in params array, like the following:
# PUT /projects/1
# PUT /projects/1.json
def update_fields
@project = Project.find(params[:project])
@project_year = params[:project_year]
@project_type = params[:project_type]
<snip>
respond_to do |format|
if @project.update_attributes(params[:project])
format.js
format.html
else
format.html { render :action => "update" }
format.json { render :json => @project.errors, :status => :unprocessable_entity }
end
end
end
The problem I have is that I’m not able to update the form, and on the server, I see the following error messages
ActiveRecord::RecordNotFound (Couldn't find Project without an ID):
app/controllers/projects_controller.rb:101:in `update_fields'
Rendered /var/lib/gems/1.8/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
Rendered /var/lib/gems/1.8/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.2ms)
Rendered /var/lib/gems/1.8/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.8ms)
If I specify the project id directly, all works fine
@project = Project.find(3)
My question is how do I get that id passed in params to the controller by the javascript function in the same way than project_year and project_type? in this example, it is 3, but it will be different for every project because it is assigned when the project is created.
Update1
<%= form_for(@project, :html => {:name => "ProjectInfo"}) do |f| %>
<input type=button id=project_info_button_id value="Show Details" onclick="showHideDiv('project_info_id','project_info_button_id');">
<div class="form input", id='project_info_id', style='display:none'>
<% concat "Project Year" %>
<%= f.text_field :ProjectYear, {:class=>"project_year_class", :Size=>6, :style=>"text-align:right"} %>
<snip>
<% end %>
<% end %>
You need to pass the project id in the data section of the ajax request: