Rails controller if else statement -


i'm working on messaging system in rails web app @ moment should give users capability see sent , received messages. sent , received messages defined in model 'to_messages' , 'from_messages'. stands, i'm able display appropriate messages both inbox , outbox. when user goes inbox , clicks on received message, show action displays content. however, not working sent messages in outbox. when user clicks on sent messages in outbox, error, suspect i'm getting because in messages controller, i'm calling to_messages(received messages). know need if/else statement in controller, i'm not sure how go writing out. apologies newbie question, have ideas? thanks!

messages_controller.rb

class messagescontroller < applicationcontroller    def index     @messages = current_user.to_messages   end    def outbox     type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"     @messages = current_user.from_messages   end    def show     @message = current_user.to_messages.find params[:id]   end    def new     @message = message.new     @recipient = user.find(params[:user_id])   end    def create     @message = message.new message_params     @message.sender_id = current_user.id     if @message.save       flash[:success] = "your message has been sent!"       redirect_to users_path     else       flash[:failure] = "please try again."       redirect_to users_path     end   end    def destroy     @message = message.find(params[:id])     @message.destroy     redirect_to messages_path   end    private    def message_params     params.require(:message).permit(:content, :sender_id, :recipient_id)   end  end 

your method outbox messages seems try handle both 'from_messages' , 'to_messages' nothing information:

def outbox   type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"   @messages = current_user.from_messages end 

once have type (from_messages or to_messages), here do:

def outbox   type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"   @messages = current_user.send(type.to_sym) end 

your 'show' action doesnt seem handling from_messages well. may necessary send type there too:

def show    type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"           @message = current_user.send(type.to_sym).find params[:id] end 

and here dry solution:

class messagescontroller < applicationcontroller    before_action :find_type, only: [:outbox, :show]    def index     @messages = current_user.to_messages   end    def outbox     @messages = current_user.send(@type.to_sym)   end    def show     @message = current_user.send(@type.to_sym).find params[:id]   end    ...   rest of controller   ...  private    def find_type     @type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"   end  end 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -