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).
So what was the task?
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.
How did I do it?
<%= 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.
What was the 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
def index
@post = Post.all
puts ">>>>#{Post.where(username:"saral")}"
end
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"
Top comments (4)
I am not sure if you are just summarizing your day or asking for specific help.
If you want help to diagnose the issue, it would probably be a good idea to publish the code on Github. This way, someone could take a peak and provide some more relevant help.
Reading over your post, it sounds like your user is instance is not defined.
Also, I would watch out for code patterns like this:
<%= f.hidden_field :username, :value => @user.username %>
In theory, anyone viewing this could change the username to a different value.
Something like this:
post = Post.new(post_params)
post.username = @user.username
if post.save
Should meet your goals and protect you long term.
-Scott
Thanks, Scott for the tips. :)
With this write up, I think I was summarizing my day, but also was seeking help. However, I don't think I gave enough for others to diagnose the issue and help me out. Will definitely use Github next time around.
Cheers
Where is project link?
Here's my project threeirblog.herokuapp.com. Although, I am still working on it. I will upload my post of Github too so that I can get better solution for my issue.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.