public
Last active

Intra user messaging

  • Download Gist
_form.html.haml
Haml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
= simple_form_for(@memo) do |f|
- if @memo.errors.any?
#error_explanation
%h2
= pluralize(@memo.errors.count, "error")
prohibited this memo from being saved:
 
%ul
- @memo.errors.full_messages.each do |msg|
%li= msg
 
.inputs
= f.input :content
 
.actions
= f.button :submit
index.html.haml
Haml
1 2 3 4 5 6 7 8 9 10 11
%h1 Listing memos
- @memos.each do |memo|
%ul
%ul
%li= memo.sender.login
%li= memo.content
- if memo == @memos.last
= render 'form'
 
%br
memos_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
class MemosController < ApplicationController
# GET /memos
# GET /memos.xml
def index
# change the index to only show chronological messages between the current user and the other user
@memos = Memo.where(
"(sender_id = ? or receiver_id = ?) and (sender_id = ? or receiver_id = ?)",
current_user, current_user,
params[:other_user],
params[:other_user]
)
 
@memo = Memo.new
@other_user = User.find(params[:other_user])
 
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @memos }
end
end
 
# POST /memos
# POST /memos.xml
def create
@memo = Memo.new(params[:memo])
@memo.sender = current_user
 
respond_to do |format|
if @memo.save
format.html { redirect_to( :action => "index", :other_user => params[:other_user]) }
format.xml { render :xml => @memo, :status => :created, :location => @memo }
else
format.html { redirect_to( :action => "index", :other_user => params[:other_user]) }
format.xml { render :xml => @memo.errors, :status => :unprocessable_entity }
end
end
end
end

i get this error after the redirect:

ActiveRecord::RecordNotFound in MemosController#index

Couldn't find User without an ID
Rails.root: /Users/jlebrech/Projects/freelance/workhorse

Application Trace | Framework Trace | Full Trace
app/controllers/memos_controller.rb:14:in `index'

The save was successful tho.

I feel its because you are not passing the params[:other_users] from your form to create method.
I am not sure about haml syntax but if you want to pass params[:other_user] from your form to create method, you can use form_tag.
I have used it many times.
Alternatively, you can create a temp table and store value of params[:other_user] in that table when you are in action 'new' and retrieve it when you are in action 'create'.

Hope this helps!
cheers.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.