Rails: an Elegant Way to Display a Message When There Are No Elements in Database

Rails: An elegant way to display a message when there are no elements in database

If you use the :collection parameter to render e.g. render :partial => 'message', :collection => @messages then the call to render will return nil if the collection is empty. This can then be incorporated into an || expression e.g.

<%= render(:partial => 'message', :collection => @messages) || 'You have no messages' %>

In case you haven't come across it before, render :collection renders a collection using the same partial for each element, making each element of @messages available through the local variable message as it builds up the complete response. You can also specify a divider to be rendered in between each element using :spacer_template => "message_divider"

5 form elements present, but only 4 are saving to the database. Rails

Remove name attribute from here:

<%= f.text_field :captcha, :required => true, :name => "captcha", :class => "message_input_default", :placeholder => " * #{@a} + #{@b} = ?" %><br><br>

It happens because name parameter is generated by rails itself, and it's responsible to structure your query. Thus this erb line:

<%= f.text_field :first_name %>

Will generate this html:

<input type="text" name="message[first_name]">

And when you submit form it will produce query like this

{ message: { first_name: 'value_of_input' } }

But you provided custom name that overridden default behaviour and produces requests like this:

{ captcha: 'captcha_val', message: { first_name: 'some_val1', last_name: 'some_val2', ... } }

Then you extract message params from params:

def message_params
params.
require(:message).
permit(:first_name, :last_name, :email, :user_message, :captcha)
end

Finally you create message with this hash:

{ first_name: .., last_name: .., email: .., user_message: .. }

In a Ruby on Rails each loop, is there a good way to do something if nothing was iterated?

You can do something like:

if @collection.blank?
# @collection was empty
else
@collection.each do |object|
# Your iteration logic
end
end

model user's message in rails 3

this is a nice problem! I would model that to compare as closely as possible to the e-mail model. So a message always belongs to a single user, and it was either sent or received.

In short:

 create_table "messages", :force => true do |t|
t.integer :user_id
t.string :subject
t.string :body
t.boolean :sent
end

And the model would like:

class Message < ActiveRecord::Base
belongs_to :user

scope :sent, where(:sent => true)
scope :received, where(:sent => false)

end

And in the user:

class User    
has_many :messages
end

You would then simply be able to query all sent messages by

user.messages.sent

and the received messages

user.messages.received

Sending a message does become a bit more complicated then:

class Message

def send_message(from, recipients)
recipients.each do |recipient|
msg = self.clone
msg.sent = false
msg.user_id = recipient
msg.save
end
self.update_attributes :user_id => from.id, :sent => true
end
end

or something along those lines: you copy the message and attach it to all recipients, and lastly make the original message the sent message.

This way each user has total control over the message.

Possible improvements:

  • also keep an explicit reference to the sender and receiver(s) in the message, to be able to allow replies and stuff
  • instead of working with a single boolean, maybe allow working with folders?

Hope this helps.



Related Topics



Leave a reply



Submit