如何在Rails的同一页面上点击用户名显示消息?

时间:2015-01-08 07:43:29

标签: ruby-on-rails

我有2个div:users_list和messages。我编写了部分代码,从发送消息的数据库用户名中进行选择。但是我该怎么做呢:

我点击了用户名,在右侧div中,它从数据库消息列,文本中选择并在此处为每个用户显示。

enter image description here

下一步怎么办?您可以写下我将回答的答案。

<% @users_list.each do |user| %>
    <li>
        <%= user.message_for %>
    </li>
<% end %>

我写了代码:

def show_message
    Message.all_messages(session[:username]).from
end

function message_for(username){
    $.ajax({
        url: '/show_message',
        method: 'POST',
        data: {
            to: username
        },
        success: function(response){
            alert(response);
        }
    })
}

Message.rb模型def:

def self.all_messages(to)
    @to = params[:to]
    Message.where(to: to).order(date: :desc)
end

然后打印下一个错误:

Started POST "/show_message" for 81.21.82.67 at 2015-01-08 08:26:29 -0500
Processing by MessagesController#show_message as */*
Parameters: {"to"=>"alizade"}
Completed 500  in 14ms

ActionView::MissingTemplate (Missing template messages/show_message, application/show_message

更新 我的路线档案:

resources :users
resources :bookworms
resources :sessions
resources :covers
resources :messages

root 'bookworm#index'

get '/profile' => 'bookworm#profile'
get '/messages' => 'messages#index'
get '/login' => 'users#login'

post '/show_messages' => 'messages#show_message'

1 个答案:

答案 0 :(得分:0)

在路线档案中定义路线

get '/show_message' => 'messages#show_message'

并遵循js代码

function message_for(username){
    $.ajax({
        url: '/show_message',
        method: 'GET',
        success: function(response){
            alert(response);
        }
    })
}