0

Here is the relevant part of the micropost view. .

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="product"><%= micropost.product %></span>
  <span class="price"><%= micropost.price %></span>
  <span class="location"><%= micropost.location %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
  <% end %>

I recently added product, price, and location to the columns in the micropost table. I checked in the database and these aspects are being recorded/written but for some reason content is the only one that gets displayed on the app. Why are the other ones left out?

Here is the generated HTML. Im assuming that this is the relevant section. as you can see the html for product, price and location is never generated.

   <li id="1">
  <a href="/users/1"><img alt="alex" class="gravatar" src="http://gravatar.com/avatar/a6759dcef8e5aad4e77509e5479b9823.png?s=50" /></a>
  <span class="user">
    <a href="/users/1">alex</a>
  </span>
  <span class="content">first</span>
  <span class="timestamp">
    Posted less than a minute ago.
  </span>
    <a href="/microposts/1" data-confirm="You sure?" data-method="delete" rel="nofollow" title="first">delete</a>
</li>

I dont think its the difference between attr_accessor and attr_accessible because content is attr_accessor and it shows up. also, I read the difference and it doesnt seem to be relevant here. I believe attr_accessible is the right one. plus I tried changing it to attr_accessor and it didnt work. Any other ideas? please and thank you!

BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
  • 1
    Can you post the generated HTML source? – Kulbir Saini Aug 13 '12 at 07:31
  • You might have switched to production and are using a different database than what you are thinking – you786 Aug 13 '12 at 23:20
  • I am viewing the app by running rails s and then visiting localhost:3000 on my browser. I believe that this means I am on development. Correct me if wrong. If so how can I switch back to development? – BigBoy1337 Aug 13 '12 at 23:24
  • Run `rails c` and then type Rails.env into the prompt and see what you get back . In your view, write `<%= Rails.env %>` and see if the two match. They should both output `development`. Also, if you want to interact with your database its usually better to use the rails console instead of working with the database directly using sqlite. – you786 Aug 13 '12 at 23:43
  • they were both development but I got it working anyways thanks! – BigBoy1337 Aug 14 '12 at 00:12

2 Answers2

1

Does the micropost that you are trying to see (ID = 1) have data populated on those fields (:product, :price, :location) ?

Luis D Urraca
  • 2,024
  • 4
  • 24
  • 46
  • +1 Generally checking the database fields is a great idea. However, in this case, even if the values were null in the database, the surrounding HTML should have shown up, so something else must be the root cause. – Nathan Aug 14 '12 at 12:44
0

Since the generated HTML doesn't even have the span tags for those fields, the issue seems to be that Rails isn't loading the newest version of your view file. Are you running in production or in development? Have you re-started the Rails server?

Also, you are correct that attr_accessible is correct rather than attr_accessor if those are database columns.

You might also check if you have any kind of caching enabled that would be displaying a cached copy of the generated HTML. For example, you could change the content value in the database and see if that is reflected in the generated HTML.

Nathan
  • 7,816
  • 8
  • 33
  • 44
  • Im not sure why it wouldnt have the new file. I am running in development (locally), and yes I have restarted the rails server. – BigBoy1337 Aug 13 '12 at 23:16
  • ok so I tried deleting the records of some of the microposts in the database (using sqlite) and checked the app and it didn reflect the changes (it still showed all the microposts including deleted ones) does this mean that the chache needs to be reset? If so, how would I do that? – BigBoy1337 Aug 13 '12 at 23:17
  • @BigBoy1337: I'm sorry, I don't know enough about Rails caching to follow up further on my suggestion. If the generated HTML is displaying content that is no longer in your database (make sure you're looking at the correct database), then it certainly sounds like a caching issue. Perhaps you can search for help with that specifically on Google and/or open a new more specific question on Stack Overflow. – Nathan Aug 13 '12 at 23:30
  • Also, caching would normally be turned off by default, especially in the development environment. If you haven't explicitly turned it on, perhaps something else is the underlying issue. – Nathan Aug 13 '12 at 23:32
  • Im an idiot, I was looking in the wrong part of the code this whole time. Thanks for helping me though. Ill pick ur answer. – BigBoy1337 Aug 14 '12 at 00:13