Here is the users show view where they are supposed to show up. ..
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<%= @sales %> <%# This is just to see if it outputs anything. It doesn't :( %>
<div id="purchases list">
<ol class="microposts">
<%= render @purchases unless @purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render @sales unless @sales.nil? %>
</ol>
</div>
so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase).
Here are the forms:
<%= form_for (@micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and
<%= form_for (@micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
also, here is the show part of the users_controller.rb
def show
@user = User.find(params[:id])
@micropost=Micropost.new
@microposts = @user.microposts.paginate(page: params[:page])
end
and here is the show part of the microposts_controller.rb
def show
@micropost = Micropost.find(params[:id])
@microposts = Micropost.where(:user_id => @user.id)
@purchases= @microposts.collect{ |m| m if m.kind == "purchase"}.compact
@sales = @microposts.collect{ |m| m if m.kind == "sale"}.compact
end
additionally, with the help of this post (http://stackoverflow.com/questions/12505845/ruby-error-wrong-number-of-arguments-0-for-1#12505865) the variables @microposts, @purchases, and @sales are all outputting correctly in the console.
can anyone help me out?
edit: using scopes as suggested by the answer given works in the console (it outputs everything correctly, but they still don’t show up in the view. Does this mean it is something wrong with my syntax for the users show page?
edit 2:
Here is the view/microposts/_micropost.html.erb code
<li>
<span class="content"><%= micropost.content %></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 %>
</li>
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.
I’m also assuming your user has many microposts:
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]’.
In MicropostsController#show, you can use your new scopes:
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).
You can also confirm expected results on rails console after creating purchases or sales micropost with:
Again, I could be missing something without seeing more of the code base.