Last active
June 7, 2016 22:01
-
-
Save mfifth/c3e9ac3f999c9124ae16b4d0c725e95c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= simple_form_for [@user, @message] do |f| %> | |
| <%= f.input :recipient %> | |
| <%= f.input :body %> | |
| <%= f.button :submit, class: "new btn-primary" %> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class='page-header'> | |
| <h1>New Message: </h1> | |
| </div> | |
| <%= render 'form' %> | |
| <br> | |
| <h3>Messages: </h3> | |
| <ul> | |
| <% @user.messages.each do |message| %> | |
| This message was sent <%= time_ago_in_words(message.created_at) %> ago | |
| from <%= message.author.email %> | |
| <li><%= message.body %></li> | |
| <% end %> | |
| <ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Message < ActiveRecord::Base | |
| belongs_to :author, class_name: "User" | |
| validates :body, presence: true | |
| validates :recipient, presence: true | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MessagesController < ApplicationController | |
| before_action :set_user | |
| def index | |
| @messages = @user.messages | |
| @message = Message.new | |
| end | |
| def create | |
| @message = Message.create(message_params) | |
| @message.author = current_user | |
| @message.recipient = User.find(params[:user_id]) | |
| if @message.save | |
| flash[:notice] = "Message has been sent." | |
| redirect_to user_messages_path(current_user) | |
| else | |
| flash.now[:alert] = "Message has not been sent." | |
| render 'form' | |
| end | |
| end | |
| private | |
| def set_user | |
| @user = User.find(params[:user_id]) | |
| end | |
| def message_params | |
| params.require(:message).permit(:body, :recipient) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| has_many :comments, foreign_key: "author_id" | |
| has_many :messages, foreign_key: "author_id" | |
| # Include default devise modules. Others available are: | |
| # :confirmable, :lockable, :timeoutable and :omniauthable | |
| devise :database_authenticatable, :registerable, | |
| :recoverable, :rememberable, :trackable, :validatable | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment