PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] how do i place this ruby code correctly?

 May 18, 2022     erb, partial, ruby, ruby-on-rails, ruby-on-rails-3     No comments   

Issue

I have a piece of code which displays a complicated 'listing' of resources. by complicated i mean that it has name, date, time posted, number of comments, picture, description, and tons more things that you obviously only want to write that block of code one time.

I'm trying to think of how this would go into the rails architecture and I'm at a loss

i need to display the resource listing using a different variable in different controllers. currently its in a partial called _resource_view.html.erb

This is fine for the main listing, but its not usable for the persons profile listing, which takes the same format but shows different resources

_resources_expanded.html.erb

<ul class="res-list">
  <% @resources.each do |resource| %>
    ... list stuff
  <% end %>
</ul>

It uses @resources on index, but on the profile page it uses the same listing code except its @user.resources.each do |resource|

I was thinking maybe something like this but this seems redundant .. and im not even sure if it will work

<ul class="res-list">
  <% @resources.each do |resource| %>
    <%= render 'layouts/resources_expanded' %>
  <% end %>
</ul>

Solution

Don't use instance variables, use local variables in the partial - that lets you pass in a single each time through the @resources.each loop.

<%= render 'layouts/resources_expanded', :resource => resource %>

and then in _resource_view.html.erb change @resource to resource



Answered By - DGM
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing