5

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(),
        }

    });
}
Community
  • 1
  • 1
rh4games
  • 962
  • 2
  • 15
  • 38

3 Answers3

2

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()
  }    
}
Jetblackstar
  • 249
  • 2
  • 12
  • Thank you very very much. Simple and exactly what I was overlooking. Solved my problem immediately. – Jeremy E Dec 28 '17 at 13:09
0

Will this work?

data: {
    "id": <%= @project.id %>,
    "project[UnitMgtAddress]": $('.unit_mgt_address_class').val(),
}
Rick Hoving
  • 3,585
  • 3
  • 29
  • 49
  • thanks for your prompt response. "id" and "unit_mgt_address" are passing fine as shown on the server logs. What I can't get working is ":project" that's required by params.require(:project).permit(:UnitMgtAddress) – rh4games Apr 30 '14 at 06:25
  • did you noticed that I add a "project" before "UnitMgtAddress", and "[]" wrap it? so it still does't works? – Raymond Liu Apr 30 '14 at 06:32
  • thanks again. I tried your suggestion and it's giving me the same result and the same error message. – rh4games Apr 30 '14 at 06:37
  • @RaymondLiu : I noticed your changes and tried them and they returned exactly the same error – rh4games Apr 30 '14 at 07:09
  • @Rich : thanks for your suggestion. I answered again to Raymond. I hope it's clear now. – rh4games Apr 30 '14 at 07:09
  • @Rashad : thanks for the clarification. I thought that's what I did in the 1st line of my question? – rh4games Apr 30 '14 at 07:11
0

You should really let Rails handle your AJAX requests for you. I'd pull that button outside of the form that's being submitted and put it into its own link_to or button_to, just add method: :get if you want to use a button. Set remote: true and it will handle all the AJAX work for you.

<%= link_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true %>

or

<%= button_to 'Get Config', get_config_projects_url(@project.id, project: { unit_mgt_address: @project.UnitMgtAddress }), remote: true, method: :get %>

Syntax might need to be tweaked slightly, for your path in particular, but letting Rails manage this sort of thing is a lot cleaner than what you're trying to do. Read up on the syntax for button_to here.

subvertallchris
  • 5,282
  • 2
  • 25
  • 43
  • thanks for your suggestion. I tried it, and it worked to some extent, after a minor change, as indicated in the update to my question under section "Working Solution" Now, I need to figure out how to replace the rest of my Javascript code with this solution. Thanks for your help. – rh4games Apr 30 '14 at 07:33
  • Great! You run `get_config` against a single project, right? If so, your ideal path would look like https://yoursite/projects/1/get_config, so your route should say `member do`, not `collection do`. – subvertallchris Apr 30 '14 at 16:12
  • in fact, this solution does not really do what I want. My form is full of buttons and input fields and I want to call get_config each time the user presses a button or changes some input, and having one button outside the form to do that does not really work for me. I'm still looking for a better way to call get_config from different places in the form, closer to my original design. – rh4games Apr 30 '14 at 16:48
  • So you have one form with multiple buttons that all make AJAX calls to the get_config action? And get_config will return updates to the existing page based on the fields entered in the form? – subvertallchris Apr 30 '14 at 17:17
  • YES, and get_config can be called also based on some input changes, not only button clicks – rh4games Apr 30 '14 at 17:26
  • OK, finally got it working with the workaround described under "Working Solution 2 (workaround)". – rh4games Apr 30 '14 at 17:47