Retrieving the correct model id - ruby-on-rails

I'm attempting to edit data from a model, but it keeps listing the User id instead of its own id. Why is that?
<% provide(:title, 'Activations') %>
<div class="row">
<div class="col-md-8">
<% if #user.activations.present? %>
<h3>Activations (<%= #user.activations.count %>)</h3>
<ol class="activations">
<%= render #activation %>
</ol>
<%= will_paginate #activation %>
<% end %>
</div>
</div>
</div>
This is part of the partial that is being rendered
<span class="user"><%= link_to activation.id, edit_activation_path %></span>
I've tried passing edit_activation_path(params[:id])The activation id is being listed correctly, but I don't know how to set the edit path with the id from the activation model.
Thank you for your help.

Try passing the model object to the link helper.
edit_activation_path(activation).

Related

I'm trying to render two columns of information in my rails app

So, I have an app that is trying render 2 (or maybe more if needed) columns using a loop. It does one column and looks pretty good, but I want the option of 2 or even more. I know about "in_groups.of()", but I can't quite figure it out to work with my
<% #vendors.each do |vendor| %>
<%= link_to vendor do %>
<div class="row">
<div class="col-md-6">
<div class="card-container">
<div class="col-md-6">
<div class="card">
<%= image_tag attachment_url(vendor, :background_image), class: 'card-img-top' %>
<div class="card-block">
<h4 class="card-title"><%= vendor.Company %>. </h4>
<p class="card-text"><%= vendor.Description.html_safe.first(25) %></p>
<div class="card-standing"><strong><%= vendor.FinancialStanding %></strong></div>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
Not sure why you needed this, because everything can be managed with css and html. But you can make changes like this:
<% #vendors.to_a.in_groups_of(2).each do |vendor| %> # #vendors.to_a cover AR to array
<% vendor.each do |v| %> # becuase vendor is array here.
..........your code..............
<% end %>
<% end %>

Issue With "Do In Groups of" In Ruby on Rails

I have the following code to display posts in groups of 3 across the page:
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3).each do |feeds| %>
<div class="row">
<% feeds.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= render feed %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>
It works fine if there are a number of posts that is evenly divisible by 3, however anything not (like 10 posts for example) generates the error "'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path."
Essentially I just want to display my posts so they look like:
Post 1 Post 2 Post 3
Post 4 Post 5
Is there a way to do this with my current code or can someone suggest a non Ruby way of doing this, for example SCSS.
Thanks!
Use Array#compact to remove the nil values:
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3).each do |feeds| %>
<div class="row">
<% feeds.compact.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= render feed %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>

How to pass an instance variable into a view partial?

Right now I have a loop that contains code I do not wish to repeat for each instance variable. I wish to put this in a partial and reuse it by changing the #new_posts instance variable, so I can use the same template for #featured_posts and #recommended_posts that I have defined.
I'm thinking of something like this:
<%= render "posts", posts: #featured_posts %>
An example of a loop that I wish to store in a partial
<% #new_posts.each do |post| %>
<div class="col-sm-4">
<div class="thumbnail">
<img src="#">
<div class="caption">
<h4><%= link_to post.title, post %></h4>
<%= post.text.truncate_words(60, omission: '...') %>
Button
</div>
</div>
</div>
<% end %>
How should I approach this? Thanks for your help!
Assign whatever variable you want to posts
<%= render "posts", posts: #featured_posts %>
And now loop over posts in the partial. You will have #featured_posts in posts variable now
<% posts.each do |post| %>
<div class="col-sm-4">
<div class="thumbnail">
<img src="#">
<div class="caption">
<h4><%= link_to post.title, post %></h4>
<%= post.text.truncate_words(60, omission: '...') %>
Button
</div>
</div>
</div>
<% end %>
For #recommended_posts render the same partial but pass #recommended_posts to posts
<%= render "posts", posts: #recommended_posts %>

link_to tag not including all divs

I currently have a link to tag which should wrap around all the content within it, but currently it's not doing that. It's wrapping around the code until it hits another div with a rails query inside it?
index.html.erb
<% #posts.each do |post| %>
<div class="widget" >
<%= link_to post do %>
<div class="image b-lazy" data-src="<%= post.image %>">
</div>
<div class="caption">
<h4><%= post.title %></h4>
<p>by <%= post.affiliate %></p>
</div>
<!-- LINK TO TAG ENDS HERE FOR SOME REASON -->
<div class="caption-top">
<% post.categories.each do |category| %>
<%= link_to category_path(category) do %>
<div class="tag <%= category.name %>"><%= category.name %></div>
<% end %>
<% end %>
</div>
<% end %>
</div>
Any help is appreciated!
Jonathan
Two things:
You are using link_to inside another call to link_to. That is probably not what you want.
The result of a block will be what you return from a block, normally the last line. Take a look at this question for a solution.

Rails passing attributes to nested modals with foundation

I have the following code. In the first modal that comes up I'm listing all of the user's images. When they click on one of the images it triggers the second modal and I want the id of the image to be available to me. But it's always coming back as the id of the last image.
<div id="firstModal" class="reveal-modal" data-reveal>
<% if signed_in? %>
<h4>Your Images</h4>
<div class="dashboard-panel">
<% #assets.each do |asset| %>
<%= image_tag asset.file_name.url(:dashboard).to_s %>
<%= asset.id %>
<% end %>
</div>
<% else %>
You don't have any images.
<% end %>
<a class="close-reveal-modal">×</a>
</div>
<div id="secondModal" class="reveal-modal" data-reveal>
<h2>This is a second modal.</h2>
<p>Asset id is <%= #selected_asset.inspect %></p>
<a class="close-reveal-modal">×</a>
</div>
I think there is something wrong with my structure but I can't figure it out :(
The html of the second modal is only generated once. The value of #selected_asset will be the id of the last asset in the loop. One option is to generate a separate modal for each image, something like this:
<% #assets.each do |asset| %>
<a href="#" data-reveal-id="secondModal-<%= asset.id %>Click</a>
<div id="secondModal-<%= asset.id %>" class="reveal-modal" data-reveal>
<h2>This is a second modal.</h2>
<p>Asset id is <%= asset.id %></p>
<a class="close-reveal-modal">×</a>
</div>
<% end %>
Note the inclusion of asset.id in the id of the modal so they are all unique

Resources