During my run this evening, I happened to listen to a podcast on codeNewbie(I have been doing this a lot lately). The guest on this episode was the amazing, the algorithm lady -I thought this was a great episode, and given what Vaidehi has achieved. Something stood out for me during this podcast though. Towards the end of the podcast, Vaidehi talks about how everyone invariably is going to get stuck at some point and how important it is to find a way to get unstuck and being ok with it. This statement resonated with me even more so today, because today was one such day for this beginner. I tried my best to get stuck, however I was only partially succesful in accomplishing the task that had me stuck.
I have been building a blog app as part of a project for learning Rails, and over the past six days, it has come along rather smoothly (apart from day #4). Today, however, was one of the biggest tests in this project (at least for now).
I don't know if I can break it down aptly enough for everyone to understand. Now that I am writing this post, the thought in my head is "Do I even understand what the task is", and if I do understand, I should be more than able to explain it to myself. So here goes, I am going to be as verbose as possible as explain it to myself first.
The task consisted of two parts
i. First, I had to pass my
session[: username], which is the username of the user currently logged in to my new blog post page.
####Why did I want to do this?
I was planning to save this username in the post model as the author of the post. The post model was my database that would store, the title of the post, the overall text of the post, and the username of the one who committed the post. This way I could track and give access to delete and edit the post to only the author of the post. Currently, because I do not have this feature, any user logged in is able to view, edit and delete the posts. This is not ideal.
This took me a while to figure out, but eventually, I did it. The first part was done.
<%= f.hidden_field :username, :value => @user.username %>
I create a hidden field for the username in the form that allowed the user to make a new post. In this case
_form.html.erb, and I passed in a default value. The create post method in the
Posts_Controller.rb was this
def create post = Post.new(post_params) if post.save redirect_to post_path(post) else redirect_to new_post_path end end private def post_params params.require(:post).permit(:username, :title , :body) end
With this task marked completed after a couple of hours of debugging, I was ready to tackle another task. I felt reinvigorated.
ii. As I began task two, I thought it was a straight forward approach, but I could not have been more wrong. As I write this post, I have still not figured my way around it. I am hoping me writing down the problem will give me another perspective on how to approach this task.
The task required me to display three links, show, edit and delete. However, as stated above the links edit and delete would only appear if the user who was currently signed in was also the author of the blog post. Here's I approached it first
<%= if @post.username == @user.username %> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Delete', destroy_post_path(post) %></td> <% end %>
My idea here was that I would get both the @post.username and @user.username to the
index.html.erb which was located in the views/posts folder from posts_controller and the sessions_controller as I had done previously. However, this straight forward approach would not work. I did a quick debug to see if I was actually getting the values I required. So, on the index page, I made a change:
<%= @user.username %>
I did this to see if I would get any return, the only return I got was an error, it read "undefined method `username' for nil:NilClass". Well, at least I now knew, my method was returning nothing at all, but the big question was why? I mean earlier in my form this very line of code seemed to work when I wanted to get the session username. I did a quick check to see if the index file and the form file were in the same directory. I deduced if they were both in the same directory it would have to work right? Wrong. They were in the same directory, but they would not work.
And so I looked on, but one of the thing that hit me as I looked on was, "What am I looking for?" what are my keywords for this search.
One of my approaches was this
@post = Post.all
In my post controller, I tried to locate the username and printed the result out in my console. However, all I got was the memory location(I am guessing). Anyway, for today, I am stuck. While getting stuck is not the greatest feeling, it definitely is an opportunity to learn. Also an opportunity to write this post.
Tomorrow, we go again. Any help or guidance is appreciated, that is ofcourse if I have been able to describe my tasks properly.
As, I conclude this post, I feel I might have a dig at the issue one more time, but then again right in front of me hangs a quote that reads "Work Hard, Be Patient"