I came from a JS background so Rails is weird to me. I currently have a show.html.erb for the contest model:
<h1>Contest name: <%= #contest.name %></h1>
<h2>Contest criteria: <%= #contest.criteria %></h2>
<h3>Photos: </h3>
<%= link_to "Enter Contest", "#" %>
<% #contest.photos.each do |photo| %>
<%= image_tag("#{photo}") %>
<% end %>
With the link_to I'm trying to render all photos that belongs to the current_user and pick one of them to assign it to the current contest. The params passing seems all so mysterious to me. Can you guys point me in the right direction of how I should tackle this problem? Thanks
What I understand you need to select an image from list of images and pass the id. So, try this
<%= label_tag "Enter Contest" %>
<% #contest.photos.each do |photo| %>
<%= link_to (image_tag("#{some photo path}")), some_controller_method_path(:photo_id => photo.id) %>
<% end %>
And in controller you can use params[:photo_id]
Related
I have Challenges containing Puns, and it is possible to vote on puns. On the Challenge Show page, all puns are rendered and show their votes count. This is currently on the view page:
<%= render #challenge.puns.reverse %>
<br>
<div id="form">
<%= render "puns/form" %>
</div>
I want the puns form to appear above the items (puns) already submitted. But if swap them around, like this:
<div id="form">
<%= render "puns/form" %>
</div>
<%= render #challenge.puns.reverse %>
I get a controller error and pun.id is not suddenly not available and the voting link breaks.
No route matches {:action=>"upvote", :challenge_id=>"9", :controller=>"puns", :id=>nil}, missing required keys: [:id]
Here is the puns/form part that is causing the issue
<% if signed_in? %>
<% if current_user.voted_for? pun %>
<%= pun.votes_for.size %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% else %>
<%= link_to like_challenge_pun_path(#challenge, pun.id), method: :put do %>
<span class="heart_like">❤</span> <%= pun.votes_for.size %>
<% end %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% end %>
<% end %>
It is the like_challenge_pun_path that throws an error but I cannot understand why. I am declaring #challenge again here, so it should be able to get the id.
Here is the form for the puns:
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
<span class=".emoji-picker-container">
<%= f.text_area :pun_text, placeholder: "Add pun", data: { emojiable: true } %>
</span>
<%= f.submit %>
<% end %>
Also, here is my routes setup
resources :challenges do
resources :puns do
member do
put "like", to: "puns#upvote"
put "dislike", to: "puns#downvote"
end
end
end
and the corresponding action to upvote
def upvote
#pun = #challenge.puns.find(params[:id])
#pun.upvote_by current_user
redirect_to #challenge
end
Can anyone help?
I think the code is for the puns collection.
I assume the issue is that in the form you have something like:
#challenge.puns.build
So in #challenge.puns collection appears not persisted record (without id), so path for this model cannot be generated.
As a quick solution I suggest:
<%= render #challenge.puns.reverse.select(&:persisted?) %>
UPDATE:
As I assumed you have
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
You can also try:
<%= form_for([#challenge, Pun.new]) do |f| %>
Or solve it in the controller. But need to see controller code for it.
I'm trying to find a way to display in a View the badge & the "granted_at" record. It would render (in the view) :
<% #profil.badges.each do |badge| %>
<%= image_tag (badge.custom_fields[:image]), badge.granted_at %>
<% end %>
Should I enable the MeritObserver to get this on the view ? Or there is a simpler solution?
(I'm on Rails 5)
EDIT
Thanks to TuteC, we have an answer :
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag (badge_sash.badge.custom_fields[:image]) %><%= badge_sash.created_at %>
<% end %>
You've got to go through sash and badges_sashes relations:
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag(badge_sash.badge.custom_fields[:image]), badge_sash.created_at %>
<% end %>
You can read about merit internals in https://github.com/merit-gem/merit/wiki/General-merit-workflow.
I'm learning RoR building social network. So in my views I have a index view which is rendering a mix of all the posts from my groups.
So now, I would like to build a link to redirect toward my original post (into his group). Redirect into the group is not really a problem, but I don't know how to redirect to my post into this group.
With a code example it will be more clear :
Index view :
<%= #posts.each do |p| %>
<%= p.title %>
Publish in <%= link_to p.group.name, group_path(p.group, ROUTE TO MY POST) %>
<% end %>
Show view(group):
<%= #group.posts.each do |post|%>
<div id='post_iter<%=post.id%>'
<%= p.title%>
</div>
<% end %>
So I would like to redirect the user toward his iteration into the group Show.
Something like
<%= link_to p.group.name, group_path(p.group, #post_iter#{p.id}) %>
This should be good :
<%= link_to p.group.name, group_path(p.group, anchor: "post_iter(#{p.id})") %>
Using two different models, RecordLabel and Artist, I want to link to their pages if the record is found using their usernames. I have no problems finding if the record exists, but I can't figure out how to find the ID of that record. What I have:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name, record_label_path(RecordLabel.find(### NEED RECORD LABEL ID ###) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
You can get the record very easily this way (if it exists):
RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first
So, your code becomes:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name,
record_label_path(RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
This should work and solve your problem.
I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.
My code:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path() %>
<% end %>
Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.
Pass tag to your route helper:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
Update:
Change tag_list to tags:
<% #post.tags.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>