0

Hello I'm trying to call a function that I wrote on my controller from my javascript as result of an action when I click on a button.

I followed that thread but it's not worked at all. When I click on the button I get the error listed bellow:

Started GET "/projects/:id/repository/:branch" for 127.0.0.1 at 2013-11-29 15:03:43 -0200
Processing by RepositoriesController#show as */*
  Parameters: {"id"=>":id", "repository_id"=>":branch"}
  ←[1m←[35m (0.0ms)←[0m  SELECT MAX(`settings`.`updated_on`) AS max_id FROM `settings`
  ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT `users`.* FROM `users` WHERE `users`.`type` IN ('User', 'AnonymousUser') AND `users`.`status` = 1 AND `users`.`
id` = 10 LIMIT 1←[0m
  Current user: guilherme.noronha (id=10)
  ←[1m←[35mProject Load (1.0ms)←[0m  SELECT `projects`.* FROM `projects` WHERE `projects`.`identifier` = ':id' LIMIT 1
  Rendered common/error.html.erb (1.0ms)
Filter chain halted as :find_project_repository rendered or redirected
Completed 404 Not Found in 24ms (Views: 21.0ms | ActiveRecord: 1.0ms)

I didn't understand well why I get this error, so I'm here to ask for help.

Bellow my code to try to detect some mistake or absence of something:

_view_button_release.html.erb

<script>
    function CallExec(rep) {
      $.ajax("/projects/:id/repository/:branch");
    }
</script>

<div class="contextual">
<% if User.current.allowed_to?(:exec, @project) %>
    <%= button_to_function l(:gerar_build_project), 'CallExec("'+params[:repository_id].to_s+'")' %>
|
<% end %>
</div>

routes.rb

resources :repositories do
  match 'projects/:id/repository', :action => 'exec_client', :controller => 'repositories', :via => :post
  match 'projects/:id/repository/:branch', :action => 'exec_client', :controller => 'repositories', :via => :post
  get :exec_client, on: :collection
end

client.rb (hook)

module InstanceMethods
  require_dependency 'repositories_controller'
  def exec_client
    begin
      ...
    end
  end
end

Any suggestion?

UPDATE:

New Log

Started GET "/projects/b1309/repository/b1309i11/" for 127.0.0.1 at 2013-12-02 10:38:00 -0200
Processing by RepositoriesController#show as */*
  Parameters: {"id"=>"b1309", "repository_id"=>"b1309i11"}
  ←[1m←[35m (0.0ms)←[0m  SELECT MAX(`settings`.`updated_on`) AS max_id FROM `settings`
  ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT `users`.* FROM `users` WHERE `users`.`type` IN ('User', 'AnonymousUser') AND `users`.`status` = 1 AND `users`.`
id` = 10 LIMIT 1←[0m
  Current user: guilherme.noronha (id=10)
Community
  • 1
  • 1
kamusett
  • 1,403
  • 1
  • 12
  • 22

1 Answers1

0

1st. In your js code

$.ajax("/projects/:id/repository/:branch");

you need insert instead of :id project identificator, instead of :branch branch id or branch name (I don't know what controller expects).

2nd. You need to pass params correctly, because I don't see any of them into log. I see params taken from url Parameters: {"id"=>":id", "repository_id"=>":branch"}. How to pass params through ajax you can google. For example you can check jquery doc and find examples.

Community
  • 1
  • 1
gotva
  • 5,919
  • 2
  • 25
  • 35
  • I've changed the appointments as you suggested but also not works. In spite of the error had disappeared, the function remains uncalled after I press the button. – kamusett Dec 02 '13 at 12:30
  • I updated the question putting the new log on the bottom of question. – kamusett Dec 02 '13 at 12:40
  • Looks like everything is OK. Your ajax request call `RepositoriesController#show` you should get this params in the action `{"id"=>"b1309", "repository_id"=>"b1309i11"}`. Now you can write logic here and return `js.erb` for example https://github.com/redmine/redmine/blob/master/app/views/issues/update_form.js.erb – gotva Dec 02 '13 at 12:45
  • Why the ajax request is calling for RepositoriesController#show? Is there a way to call the fuction exec_function I've written for this button? – kamusett Dec 02 '13 at 12:51
  • I think because your `CallExec` js function calls `$.ajax("/projects/:id/repository/:branch");` – gotva Dec 02 '13 at 12:53
  • So you place a button to a page. When the button is pressed you call js function`CallExec` with some params. This js function calls `RepositoriesController#show` through ajax. Everything works as expected... What is wrong? – gotva Dec 02 '13 at 19:51
  • It is not as expected. The expected is to call RepositoriesController#exec_client – kamusett Dec 02 '13 at 20:16
  • You define the route through `:via => :post` but send a get request (look at the log `Started GET...`). Check [type section](http://api.jquery.com/jQuery.ajax/) – gotva Dec 03 '13 at 06:35