Ror: How to Get My Microposts to Show Up

RoR: How can I get my microposts to show up?

I'm making some assumptions without seeing more of your code, but it looks like you could
write what you've shown a little differently. I'm assuming your databases are migrating
and have the required columns, e.g., Micropost#kind, Micropost#user_id, etc.
You can use scopes to refine a collection of microposts more expressively. It might be helpful to read
up about ActiveRecord scopes: http://guides.rubyonrails.org/active_record_querying.html#scopes.

class Micropost < ActiveRecord::Base
belongs_to :user

scope :purchases, where(:kind => "purchase")
scope :sales, where(:kind => "sale")

# your code
end

I'm also assuming your user has many microposts:

class User < ActiveRecord::Base
has_many :microposts

# your code
end

For your forms, I'd suggest attaching your hidden field to the form object (f.hidden_field) so
you don't have to specify the name as 'micropost[kind]'.

<%= form_for(@micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field :kind, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

In MicropostsController#show, you can use your new scopes:

def show
@micropost = Micropost.find(params[:id])
@microposts = @user.microposts
@purchases = @microposts.purchases
@sales = @microposts.sales
end

You should also confirm that your MicropostsController#create action is actually adding
the microposts to the user sending the form (I'm assuming a current user method).

def create
@micropost = current_user.microposts.create(params[:micropost])
# yada
end

You can also confirm expected results on rails console after creating purchases or sales micropost with:

Micropost.purchases
Micropost.sales

Again, I could be missing something without seeing more of the code base.

RoR: How can I get all of my users microposts to show up in one place?

Your XController will look for a view template in app/views/X, so you need to move your home.html.erb out of app/views/static_pages and into to either app/views/users or app/views/microposts. Also, why do you have identical home methods in both controllers?

If that doesn't work for you I'd want to know (a) what URL you're browsing to, and (b) what's in your routes.rb file.

RoR: why aren't my microposts showing up?

First, just to be sure you are getting the results you want, you should try something like this in your view

<%= @sales %>

This should be a hash of the results you want. Then, if that looks good, you want to do something like this

<div id="sales_list">
<ol class="microposts">
<% if @sales.any? %>
<% @sales.each do |sale| %>
<li><%= sale %></li>
<% end %>
<% end %>
</ol>
</div>

And repeat for purchases

RoR: how can I list only microposts that have a certain attribute in one column?

Controller:

@purchases = @microposts.where(:kind => 'purchase')
@sales = @microposts.where(:kind => 'sale')

View:

<h2>Purchases</h2>
<%= render @purchases %>

<h2>Sales</h2>
<%= render @sales %>

RoR: How do I define @microposts two different ways in the same view?

when the page is initially loaded, i guess the params[:search] was not present. so you can try:

if params[:search].present?
@microposts=Micropost.search(params[:search])
else
@microposts=Micropost.all

How can I expand the micropost model from Hartl's Ruby on Rails tutorial?

http://sourceforge.net/projects/sqlitebrowser/

download that, run that application. open your db... it'll be wherever your app is... then db/development.sqlite3

click on the browse data tab. switch to your microposts. are your new columns in there? probably not...

my guess is you just went into your migration files and started tinkering. no no.

once you run a db:migrate, your database is built. any changes that you want to make have to be added in the form of a new migration or rollback.

so in your case... you'll probably want to to run rails g migration add_price_to_microposts, which will create a new file under db/migrate.

class AddPriceToMicroposts < ActiveRecord::Migration
def change
add_column :microposts, :keyword, :string
add_column :microposts, :price, :integer
add_column :microposts, :condition, :string
end
end

your other option is to rollback. rake db:migrate:down VERSION=20131130180735. this will kill your table... but that's probably ok in your case. now you can go back into that migration file... make the changes that you want in the form of t.sting :whatever, then when you're content, rake db:migrate to rebuild the table.



Related Topics



Leave a reply



Submit