Search code examples
ruby-on-railsviewposting

Displaying Important To Do's In Index View


Fresh learning rails, be gentle. No programming experience, but learning.

Building simple app: An app that asks "what's the most important thing you can do right now," gives you an answer field, submits it, and then displays the stored important things.

Ideally, they won't be stored on the index page, but for learning purposes, I'm trying to get them to do this.

Controller code:

class FacilitatesController < ApplicationController

  def index
    @facilitate = Facilitate.all
  end

  def new
    @facilitate = Facilitate.new
  end

  def create
    @facilitate = Facilitate.new(params[:facilitates])
    @facilitate.save
    redirect_to @facilitate
  end

  private

  def facilitate_params
    params.require(:facilitate).permit(:answer)
  end

  def show 
    @facilitate = Facilitate.find(params[:id])
  end

end

Index View code:

<h1>Impactful Task Elicitation</h1>

<h1>Listing Stored To dos</h1>

<table>



</table>

<%= link_to 'Store impactful tasks', new_facilitate_path %>

NEW view code:

<h1>What is the most impactful task?</h1>

<p>Store below, motherbiatch</p>

<%= form_for :facilitate, url: facilitates_path do |f| %>

  <p>
    <%= f.label :answer %><br>
    <%= f.text_area :answer %>
  </p>

  <p>
    <%=f.submit 'Save Task' %>
  </p>
<% end %> 

So far, I can navigate from index, to facilitates/new, and answer the question, to store my important to do. It then takes me to facilitates/33 (ID I'm assuming, or the number that I'm on, task wise)

I'd like to display these tasks both on the facilitates/33 (or whatever number it ends up being) page, as well as the index page.

I've followed directions on a similar type of form here: http://guides.rubyonrails.org/getting_started.html but, I still can't get my stored To do's to display anywhere.

Any help would be awesome.



Solution

  • In your controller, you have the show method below the private line. That means that it can only be called from inside the controller, so you are being sent to the show template without that method ever being called (@facilitate will be nil).

    Move the def show method up above the private line.


    It then takes me to facilitates/33 (ID I'm assuming, or the number that I'm on, task wise)

    The line redirect_to @facilitate, means that after the facilitate is created, go to it's show method and page. The 33 is just a database reference for that particular facilitate, that it can be looked up again with Facilitate.find(params[:id]).

    You didn't post what app/views/facilitates/show.html.erb looks like, but if you want to display the newly created facilitate, then it should have a line like this:

    <%= @facilitate.answer %>
    

    I'd like to display these tasks both on the facilitates/33 (or whatever number it ends up being) page, as well as the index page.

    If you only care about the listing, and not individual facilitates pages, then after creation you can redirect back to the index in the create method by changing redirect_to @facilitate to redirect_to facilitates_path (which translates to '/facilitates').


    EDIT:

    The <%= @facilitate.answer %> example was meant for the show view, not index. On index, you'd do something more like this:

    <% @facilitate.each do |facilitate| %>
      <%= facilitate.answer %><br>
    <% end %>
    

    To list them all.