I added the URL parameters to the new.html.erb page by a link_to
<%= link_to 'Message', new_personal_message_path(receiver_id: 1010) %>
which correctly to displayed in the URL as
example.com/personal_messages/new?receiver_id=1010
and i was able to than reference it in my controller by simple @receiver = User.find_by(id: params[:receiver_id])
On the new.html.erb i have this form to create a new conversation:
<%= form_for @personal_message do |f| %>
<%= hidden_field_tag 'receiver_id', @receiver.id %>
<%= f.text_area :body, class: "personal_message_textarea", placeholder: "Chat with us...", :autofocus => true %>
<%= hidden_field_tag :conversation_id, params[:id] || session[:conversation_id] %>
<%= f.submit " ", placeholder: "Chat with us!", class: "personal_message_submit" %>
<% end %>
And it'll automatically redirect the user to the show page which the url displays as
example.com/conversations/1
But I also want it to also show the newly created user_id (I have a method in my controllers that automatically creates a user account on create) in the URL parameters like so:
example.com/conversatinos/1?user_id=23&receiver_id=1010
the new method in personal_messages controller
def new
redirect_to conversation_path(@conversation) and return if @conversation
@personal_message = PersonalMessage.new
@site = Site.find_by(id: cookies[:siteid]) #used to pull site description
end
create method in personal_messages controller
def create
@conversation ||= Conversation.create(author_id: cookies[:user_id],
receiver_id: @receiver.id)
@personal_message = current_user.personal_messages.build(personal_message_params)
@personal_message.conversation_id = @conversation.id
@personal_message.save!
flash[:success] = "ok!"
redirect_to conversation_path(@conversation)
end