0

First we start off with conversations/index.html.haml to create a message

#new_message_conversation
            .panel.panel-info
                .panel-heading
                    %h4 Send a Bark!
                .panel-footer(style="padding-top: 20px")
                    = simple_form_for :message, url: :messages, :remote => true do |f|
                        .form-group
                            = f.input :master_name, placeholder: 'Choose master...', label: false, :url => autocomplete_master_name_conversations_path, :as => :autocomplete, id_element: "#master_name_id", input_html: {class: "form-control"}

                        = f.input :recipient_id, as: "hidden", input_html: {id: "master_name_id"}

                        = f.input :body, label: false, as: "text", placeholder: 'Write message...', :input_html => { :rows => 5 }
                        = f.button :submit, 'Send', :class => "btn btn-lg btn-primary", :disable_with => "Sending..."

which then goes to the messages#create action which has

...
    respond_to do |format|
              format.js { render "create", locals: { conversation: @conversation, conversations: @conversations, receipts: @receipts }}
    end
...

which sends the work to the conversations/create.js.erb file

$('#new_message_conversation').prop('disabled', true).html("<%= raw escape_javascript(render(:partial => 'conversations/show', locals: { conversation: conversation, receipts: receipts })) %>").hide().fadeIn('fast');

which adds the conversations/show partial, _show.html.haml which has

...    
%ul.pager.pull-left(style= "padding-left: 10px")
    %li#paginator_3= link_to_previous_page @receipts, "Newer", :remote => true, :param_name => 'page_2'
    %li#paginator_4= link_to_next_page @receipts, "Older", :remote => true, :param_name => 'page_2'
...

everything works excepts now the pagination buttons don't work. and when I click a pagination button the server says

Rendered messages/index.js.erb 

Why does a partial that's in views/conversations that has a remote ajax call render to a different controller (messages)? It should be rendering conversations/show.js.erb because the partial is conversations/_show.html.haml right?

here are my routes also

...
resources :conversations do
    get :autocomplete_master_name, :on => :collection
end
resources :messages
...
monty_lennie
  • 2,871
  • 2
  • 29
  • 49

1 Answers1

2

Even though you're rendering views and partials from the conversations path, you never even touched the ConversationController.

You can render whatever views you want for the action you're executing. The only thing connecting the ConversationController with the views/conversation/file.html.erb and similar view files is a loose naming convention. When rendering say render 'index' from an action of the ConversationController, it just assumes by that convention you meant the views/conversation/index.html.erb file.

Your view or partial alone cannot reference the controller it would belong to (when going by the naming convention) because it is just used as a template by the render command in your action. The view doesn't care whether the controller behind it is the appropriate one. In this case, the render was originally executed in the MessagesController, so the view also just has a reference to that one.

To still have the links point to the correct controller, you need to specify the controller to be used for the url. Otherwise, it is assumed that you want to use the very same controller you used to render the page.

The culprit is probably somewhere in the link_to_previous_page and link_to_next_page helpers by kaminari. When using the full paginate helper, you can set the controller and action you want to use like this:

<%= paginate @users, :params => {:controller => 'foo', :action => 'bar'} %>

The documentation (here: https://github.com/amatsuda/kaminari) doesn't say whether this param is also possible with the other helpers, but the helper uses a simple link_to (see here: https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/helpers/action_view_extension.rb), so you should be able to do something like this:

link_to_previous_page @receipts, "Newer", {:controller => 'foo', :action => 'bar', :remote => true, :param_name => 'page_2'}
KappaNossi
  • 2,656
  • 1
  • 15
  • 17