OK, after I fixed this issue, now I have this new one, which I don't seem to get my head around.
This is my view and Javascript code:
<script language="javascript" type="text/javascript">
function getConfig(){
$.ajax({
url: "<%= get_config_projects_url %>",
data: {
id: <%= @project.id %>,
unit_mgt_address: $('.unit_mgt_address_class').val(),
}
});
}
</script>
<%= form_for(@project) do |f| %>
<input type=button onClick="getConfig()"/>
<div class="field">
<%= f.label :Unit Management Address %><br>
<%= f.text_field :UnitMgtAddress, :class=>'unit_mgt_address_class' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My Controller
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
<many other methods go here>
# GET /projects/1
# GET /projects/1.json
def get_config
@project = Project.find(params[:id])
respond_to do |format|
if @project.update(project_params)
format.js
format.html { render action: "edit", notice: 'Project Get_Config Successful!' }
else
format.html { render action: "update" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:UnitMgtAddress)
end
end
When I click on "Refresh" button, I get this error message:
Started GET "/projects/get_config?id=9&unit_mgt_address=5.5.5.5" for 127.0.0.1 at 2014-04-29 22:48:37 -0700
Processing by ProjectsController#get_config as */*
Parameters: {"id"=>"9", "unit_mgt_address"=>"5.5.5.5"}
Project Load (1.0ms) SELECT "projects".* FROM "projects" WHERE "projects"."id"= ? LIMIT 1 [["id", 9]]
Completed 400 Bad Request in 6ms
ActionController::ParameterMissing (param is missing or the value is empty: project):
app/controllers/projects_controller.rb:128:in `project_params'
app/controllers/projects_controller.rb:91:in `block in get_config'
app/controllers/projects_controller.rb:89:in `get_config'
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (63.0ms)
I tried many ways to pass ":project" parameter to the controller via Javascript, but I had no luck.
Any help is appreciated?
Thanks & Best Regards
Update 1 As requested, these are the routes I added for get_config and set_config methods
resources :projects do
collection do
get :get_config
get :set_config
end
end
Working Solution 1
This is the code change that worked for me : I moved the button outside the form and replaced it with the following
<%= button_to 'Get Config', get_config_projects_url(id: @project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>
Working Solution 2 (workaround)
function getConfig(){
$.ajax({
url: "<%= get_config_projects_url(project: { id: @project.id, unit_mgt_address: @project.UnitMgtAddress }) %>",
data: {
id: <%= @attero.id %>,
unit_mgt_address: $('.unit_mgt_address_class').val(),
}
});
}
For anyone viewing this many years later. I think you needed to put a "project" object param under your data to encompass your intended params. For anyone else it's the object name (:page, :person etc)
Rails expects this in its controllers.
so;
data: {
project: {
id: <%= @attero.id %>,
unit_mgt_address: $('.unit_mgt_address_class').val()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With