Day 80 - #100DaysofCode - Stuck On A Rails Problem
Tags: 100daysofcode, help, rails, ruby
Published: 2020-08-20 02:16:32

If you keep up with my #100DaysofCode, you will know that I am working on another rails application to show on my portfolio. Today I focused on the application and made a few updates. 

{% github SincerelyBrittany/photo-app no-readme %}

I am still using the <a href="https://github.com/unsplash/unsplash_rb"> unsplash gem</a> to display images on my application. The goal is to allow a user to search for a picture and then if they like it, they can use a check box and add it to their profile. Seems simple, but I keep running into an issue.

First, I iterate over the Unsplash gem and create a new Photo instance.

```
   photos = Unsplash::Photo.search("cats")
        @photos = []
        photos.each do |p|
           a = Photo.new(photo: p["urls"].full, quote: p["alt_description"] )
           @photos << a
        end 
```

Then, I display it on the index show page so that a user can view the photos:

```
<%= form_tag(new_photo_path, {:method => :get}) do %>
    <% @photos.each do |p| %>
            <img src="<%= p.photo %>" alt="random" ><br>
            <div><p> <%= p.quote %> </p></div><br>
            <%= check_box_tag "photo_ids[]", p , true%>
    <% end %>
  <%= submit_tag "Submit Post" %>
<% end %>
```

Above ``p`` is equal to  ``#<Photo id: nil, photo: "https://images.unsplash.com/photo-894384059358603-a3...", quote: "selective focus photography brown cat lying over b...", created_at: nil, updated_at: nil>``

The issue occurs when I get to the controller. I put a ``byebug`` in my ``new`` method and when I check the params I get the following:

```
(byebug) params
<ActionController::Parameters {"photo_ids"=>["#<Photo:0x00007f9620c97d28>", "#<Photo:0x00007f961bb58278>", "#<Photo:0x00007f9620880528>", "#<Photo:0x00007f9620a54160>", "#<Photo:0x00007f961cd44cc0>", "#<Photo:0x00007f9620d86fb8>", "#<Photo:0x00007f961ba078d8>", "#<Photo:0x00007f961cbd5b50>", "#<Photo:0x00007f961cc34f88>", "#<Photo:0x00007f9620c25a48>"], "commit"=>"Submit Post", "controller"=>"photos", "action"=>"new"} permitted: false>
```

It appears that the photos are the Photo objects that have not been saved yet, which I thought was great, Ill just save them in the method right? WRONG! I have no idea how to save it from here. I have tried saving it into a variable and then saving. I have tried referencing the items within the object. 

```
(byebug) a = params[:photo_ids][1]
"#<Photo:0x00007f961bb58278>"
(byebug) a.save
*** NoMethodError Exception: undefined method `save' for "#<Photo:0x00007f961bb58278>":String

nil
(byebug) 
```

No luck. It looks like the checkbox is sending it as a string as opposed to the object itself. I know I can not find the object based off the ``id`` since the ``id`` does not exist yet. Is there a way around this or just a better way to do this process in general?  

I am trying to avoid creating/saving the unsplash items immediately to my database, I do not want to save until after a user has selected it. I will try again tomorrow, hopefully find a way to get this task done. 

<b> Fun Fact </b>: For the past few days my rails console was not working. I thought it was my computer but then I googled: "rails console not working"

Saw the following <a href="https://stackoverflow.com/questions/24276033/rails-console-doesnt-start"> Stack Overflow Question </a>

Ran ``spring stop`` and then ``rails console`` and my console was working again. I have no idea what ``spring stop`` does but I wanted to share for any other code newbies who may run into this issue. 

As always, thanks for reading!

Sincerely,
Brittany

Song of the Day:

{% youtube fhbjslTsauM %}