I'm sure I'm missing something simple, but I'm generating a list of projects each with a form to add a new audition to that particular project. The form isn't being generated properly.
The view:
<% #projects.group_by(&:aasm_state).each do |state, projects| %>
<div class="project-column small-order-<%= state_priority(state) %>">
<div class="project-state spaced-out-2 project-<%= state %>"><%= state.titleize %></div>
<% for project in projects %>
<%= render partial: 'auditions/partials/dash_project_audition_form', locals: {project: project} %>
<% end %>
<% end %>
This is the code in the partial:
<button data-click="new-project-audition">+ Audition for <%= project.title %></button>
<%= form_for([project, #audition]) do |f| %>
... SNIP....
<% end %>
Everything works except the form. Each form generated contains the ID for the last record in #projects. So, for example the last project is ID 19, no matter which project form I click on, they're all for 19. The problem isn't with the partial, it's with something I'm doing in form_for, because + Audition for <%= project.title %>, which is inside the partial, generates the correct title.
UPDATE:
I restarted the server and form_for is now generating the right path, with the right ID.
I have this line also inside the form_for loop, and it's still wrong.
<h3 class="section-head">New Audition for <%= project.title %></h3>
It's generating the title for the last project in the whole loop.
UPDATE 2: Stopped working on a page reload, so... not even sure what to say now.
when you do form_for([project, #audition]), it generates a form for
projects:project_id\auditions:audition_id
so the id that you see in all forms may belong to the #audition variable. Where do you initialize it ?
It should be initialized ideally on a per project basis, right ? Let me know if this helps in any way.
Related
I have a model subscription_tier with show and edit actions with corresponding views. I've wrapped each of these with a turbo frame
<%= turbo_frame_tag subscription_tier do %>
When I edit an existing subscription tier and save it, the turbo frame refreshes and shows my saved tier, but I'm unable to replicate this for creating new tiers.
My new tier frame:
<turbo-frame id="new_tier">
<%= link_to "Add Tier", new_create_subscription_tier_path(sub_type: "Free"), class: "btn btn-primary mb-3 fs-6"
%>
</turbo-frame>
and in new.html.erb
<turbo-frame id="new_tier">
<%= render partial: "create/subscription_tiers/edit", locals: {
subscription_tier: #subscription_tier
} %>
</turbo-frame>
Clicking Add Tier button successfully renders the form and I can save the object, but on save the turbo frame is destroyed with the error Response has no matching <turbo-frame id="new_tier"> element
I know this is because my show partial is wrapped with the <%= turbo_frame_tag subscription_tier do %>, but I don't know how to reconcile this.
I believe you need to add a data tag to your new link i.e.
<%= link_to "new subscription", subscription_path, data: { turbo_frame: dom_id(Subscription.new) } %>
On the same page as this link ^^^^ you need to add
<%= turbo_frame_tag Subscription.new %>
I believe this is what your trying to accomplish
There is a fantastic tutorial that goes over all this stuff that is free here
Perhaps the OP figured this out, but for anyone looking for a possible solution, you could always set the turbo frame ID manually, for example:
<!-- new.html.erb, edit.html.erb -->
<%= turbo_frame_tag 'subscription_tier_frame' do %>
<%-- form for subscription tier -->
<% end %>
and
<!-- show.html.erb -->
<%= turbo_frame_tag 'subscription_tier_frame' do %>
<%-- rendered subscription tier -->
<% end %>
That'll keep things in the same turbo frame.
I am a beginner programmer. I'm building my first project which shows a list of my favorite athletes and when clicked the "show" page loads an image of them, their name, and weight.
Overall when adding a new athlete everything works fine. If I decide to add a new athlete and skip adding an image I get this error "Can't resolve image into URL: undefined method `persisted?' for nil:NilClass", it also highlights my code "<%= image_tag(#fighter.image) %>" that's in my show.html.erb
I've tried adding an alt: but I get another error of "wrong number of arguments (given 1, expected 0)".
I've searched for answers and one thread mentioned my controller page and the params section. I did notice that (:image) is preceded by "params.require" which I would assume could be the problem if I'm not adding an element that is required. How do I solve this issue? What would be the best way to add a placeholder, I've added a default.png in my images folder.
Thank you
show.html.erb
fighters_controller.rb
If you're using ActiveStorage for uploading athlete images. You can check if an image was attached to your model with #fighter.image.attached?.
So I would suggest wrapping the image_tag in an IF-clause like so:
<% if #fighter.image.attached? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
If you're using Paperclip then try the following:
<% if #fighter.image.exists? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
With Carrierwave use:
<% if #fighter.image.present? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>
Have a show action on a ticket that pulls in a list of comments on the ticket. My goal is to display the list of ticket comments and then above it give the option for someone to add a comments.
This works when the form is below the list of comments, however when i put the above the comments, i get an error in the render of the comments saying that the record has no user id. Almost like the #comments in my ticket controller is somehow getting this new thing added from the form even though it is instantiated before the render of the form.
Here are my partials and controller. The error when the form is displayed first is "unable to find user_id = 0" That is due to the comments.commenter, which looks for the name of the person submitting the comment.
Ticket Controller
def show
#ticket = Ticket.find(params[:id])
#comments = #ticket.comments
end
tickets/show - The form is in here twice, but i only put it in once when trying to get this to work. I want it to work in the top spot.
<div class="widget-content nopadding">
<ul class="new-comment">
<%= render "comments/form" %> --- Does not work here
</ul>
<ul class="recent-comments">
<%= render #comments %>
</ul>
</div>
<%= render "comments/form" %> --- Works here,
comments/form
<%= form_for([#ticket, #ticket.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
comments/comment
<li>
<div class="comments">
<span class="user-info"> User: <%= comment.commenter %> </span>
<p>
<strong>Comment:</strong>
PUBLIC: <%= comment.try(:content) %>
</p>
</div>
</li>
Do you have a default set on the comment user_id to 0? What's happening is the form is building a new comment on the ticket. So i think that is getting rendered in the collection partial in addition to what the ticket already had. With the user_id set to zero, the comment.commenter tries to do the where id = 0, and it blows up.
So if you do have a default on that column, remove it. Foreign keys should default to nil.
The #comments is likely lazy loaded, that's why the _comment partial can be effected. #comments was not invoked until you have the second render. To avoid this, you can switch the #comments in the controller to:
#comments = Comment.where(ticket_id: params[:id])
Hope this made sense. Let me know if the default is the case. It's just a hunch :)