Search code examples
ruby-on-railsrubymessaging

I want to use :username in the field instead of :user_id


I am beginner in ruby on rails. I am building a forum application. In addition to post and comments on posts, I want the private messaging system in the application. The message should be sent to the desired user("only recipient can see the message"). For that I generated a model Notification having message as a column.

Notification Model

class Notification < ActiveRecord::Base
    belongs_to :user
end

Notification Migration

class CreateNotifications < ActiveRecord::Migration
  def change
    create_table :notifications do |t|
      t.text :message  
      t.integer :recipient_id, class_name: "User"
      t.timestamps null: false    
      t.references :user, index: true, foreign_key: true    
    end   
  end
end

Notification Controller

class NotificationsController < ApplicationController


    def index
        @notifications = Notification.all.order("created_at DESC")

    end

    def new
        @notification = @notification.new
    end

    def create
        @notification = @notification.new notification_params
        if @notification.save
            redirect_to(:controller => "posts", :action => "index")
        else
            render "new"
        end
    end

    private

    def notification_params
        params.require(:notification).permit(:message, :user_id, :recipient_id)
    end

end

Notification#new view

<%= form_for(:notification, :url => {:action => "create"}) do |f| %>

    <%= f.text_field(:message, :placeholder => "Enter your message") %>
    <%= f.number_field(:recipient_id, :placeholder => "Enter the recipient") %>
    <%= f.submit("send message") %>

<% end %>

This is working. But I have to enter the :user_id in the :recepient_id field. What I want is I want to fill the field with username(recipient name) instead of filling :recipient_id. Please help me. I appreciate your answers. Thanks in advance.


Solution

  • My suggestion are as below - I have not tested this code, hence, use this code for illustration.

    Use text field for recipient identification so that user can enter recipient name:

    <%= f.text_field(:recipient_name, :placeholder => "Enter the recipient") %>
    

    And in your controller, update notification_params to allow recipient_name param.

    def notification_params
        params.require(:notification).permit(:message, :user_id, :recipient_name)
    end
    

    Also, in create method, you can look up the Recipient:

    def create
    
        # Look up user corresponding to the name.
        u = User.find_by(name: params[:recipient_name])
    
        # Add recipient_id to params
        notification_params = notification_params.merge({recipient_id: u.id})
    
        @notification = @notification.new notification_params
        if @notification.save
            redirect_to(:controller => "posts", :action => "index")
        else
            render "new"
        end
    end