Skip to content

Instantly share code, notes, and snippets.

@jlebrech
Created May 19, 2011 14:13
Show Gist options
  • Save jlebrech/980828 to your computer and use it in GitHub Desktop.
Save jlebrech/980828 to your computer and use it in GitHub Desktop.
Intra user messaging
= 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
%h1 Listing memos
- @memos.each do |memo|
%ul
%ul
%li= memo.sender.login
%li= memo.content
- if memo == @memos.last
= render 'form'
%br
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
@jlebrech
Copy link
Author

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.

@SauronTheGreat
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment