1
votes

I want the next thing: when the button is clicked, I want to print the params[:id] of the welcome controller.

this is my java script code:

$(".btn_skip").click(function() {
    $.ajax({
        url: '/welcome',
        type: 'PUT',
        data: {show_msg: $("#mycheckbox").is(":checked")}
    });
});

and this is my welcome_controller.rb:

class WelcomeController < ApplicationController
def update
    @user = User.find(params[:id])
    puts params[:id]
    end
end

when I ran: rails s and pressed the button (btn_skip), I got:

Started PUT "/welcome" for 127.0.0.1 at 2013-02-05 12:18:26 +0200

ActionController::RoutingError (No route matches [PUT] "/welcome"):

routes.rb:

resources :welcome

rake routes:

welcome_index GET    /welcome(.:format)          welcome#index
              POST   /welcome(.:format)          welcome#create
new_welcome   GET    /welcome/new(.:format)      welcome#new
edit_welcome  GET    /welcome/:id/edit(.:format) welcome#edit
welcome       GET    /welcome/:id(.:format)      welcome#show
              PUT    /welcome/:id(.:format)      welcome#update
              DELETE /welcome/:id(.:format)      welcome#destroy

maybe, should I pass the id in url: '/welcome'?

if so, how can I get the id? maybe by: url: '/welcome/#{:id}'?

any help appreciated!

2
Yep you need to pass the id along. you can get the id as <%[email protected]%> assuming u are using erb templates for views with Javascript where @something is the object that you pass to /welcome view file to render.redDragonzz
ok, thank you, but how can I pass it? something like: '/welcome/#{:id}' ?Alon Shmiel
do u retrieve the object on the welcome page? you can get the id thenredDragonzz
it's in: index.html page..Alon Shmiel
I have a div class in the controller of board: <div class="simple_overlay" id="welcome_dialog"> that I show it in the popup window, by the lines: $("#welcome_dialog").lightbox_me({ centered: true, onLoad: function() { //alert("Page is loaded"); } }); so I don't know how to send the id to the welcome controllerAlon Shmiel

2 Answers

1
votes

I would recommend you to use rails form helper instead of plain html. Anyways Its your choice, with the current implementation you can add a hidden field to the form. I am assuming you want to pass the current_user's id to update action.

<input type="hidden" id="current_user" value="<%= current_user.id %>" />

Now in your javascript code:

$("#submit").click(function() {
  user_id = $('#current_user').val();
  $.ajax({
    ## the url is the controller
    url: '/welcome' + user_id,
    type: 'PUT',
    data: {show_msg: $("#mycheckbox").is(":checked")}
  });
});

Thats it! It will fetch the user id and will pass to update action auto-magically. :)

2
votes

If you generating Javascript using the html.erb templates then you use the inbuilt url generators. e.g.

This is my Index.html.erb

<% @device_layouts.each do |device_layout| %>
  <tr>
    <td><%= link_to 'Show', device_layout %></td>
    <td><%= link_to 'Edit', edit_device_layout_path(device_layout) %></td>
    <td><%= link_to 'Destroy', device_layout, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>

where edit_device_layout_path(device_layout) will automatically form the Url for me.

So let's say I am actually using javascript and i have multiple edit button for each of the items above then the code might look this:

    <% @device_layouts.each do |device_layout| %>
      <tr>
        <td><%= link_to 'Show', device_layout %></td>
        <td>
 <script type="text/javascript">
    $(".btn_edit").click(function() {
        $.ajax({
            url: '/welcome/<%=device_layout.id%>',
            type: 'PUT',
            data: {show_msg: $("#mycheckbox").is(":checked")}
        });
    });
</script>
<div class="btn_edit">Edit Me</div>
</td>
        <td><%= link_to 'Destroy', device_layout, confirm: 'Are you sure?', method: :delete %></td>
      </tr>
    <% end %>