How to pass html for a link_to block? - ruby-on-rails

I'm trying to pass a link_to block with html but can't get it. I tried some other ways with no luck so I will use my original code:
<% link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
This doesn't show the :class though.
What needs to be corrected?

Try to add = to make it <%= %>
<%= link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
In the view code in Rails 3 applications it’s sometimes necessary to
use <%= instead of <% at the beginning of blocks that output content,
such as form_for.
Since it's just a span, why don't you just do
<%= link_to "add questions to #{survey.name}", survey_path(survey), :class => "button" %>

Related

Rails - Link a thumbnail image to a larger full image

I'm new to Rails and I'm trying to decipher how to link an image to open a different image (thumbnail vs large image, and they aren't the same).
I've tried:
<%= link_to "fullNews.jpg" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
As well as trying to use erb inside of erb:
<%= link_to "<% image_tag("fullNews.jpg") %>" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
Even a direct link:
<%= link_to "assets/images/fullNews.jpg" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
I'm sure it's just a syntax thing but I can't figure out how to achieve this?
Pretty easy:
<%= link_to image_url("fullNews.jpg") do %>
<%= image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>

Using a Form_for with an each loop

I'm trying to create a form in Rails that allows a user to select certain photos from a larger list using checkboxes. Unfortunately, I haven't found any similar examples and most posts out there are unhelpful. Any ideas on how to solve this?
<div>
<%= form_for :photos, url: trip_path, method: "PUT" do |f| %>
<% #photos.each_with_index do |image, index|%>
<img src="<%= image.url %>" ><br>
<span> <%=image.caption%> | <%=image.lat %> | <%= image.long %>
<%= f.hidden_field "url", :value => image.url %>
<%=check_box_tag('photo') %>
</span>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
</div>
API docs states that a form_for
creates a form and a scope around a specific model object
so, you cannot use it with a collection.
A possible way to do it, is to use the form_tag instead of form_for and check_box_tag (which you already have).
The behavior you've depicted is categorically impossible using form_form. However, if you're willing to forgo form_for (and there's no reason why you shouldn't, given your criteria), you can imitate the behavior depicted by nesting a foreach loop – each loop containing a form_for block – within a form_tag:
<div>
<%= form_tag trip_path, method: "PUT" do |f| %>
<% #photos.each do |photo|%>
<img src="<%= photo.url %>" ><br>
<span> <%= photo.caption%> | <%= photo.lat %> | <%= photo.long %>
<%= fields_for "photos[#{photo.id}]", photo do |p| %>
<%= p.hidden_field 'url', :value => photo.url %>
<%= p.check_box 'photo'
<% end %>
</span>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
</div>

adding a css inside a link_to do function

I'm trying to make link_to function have a css class attached to it,
My code is something like this:
<%= link_to newGame do%>
<%= image_tag newGame.image_url.to_s %>
<% end %>
which links an image to its content, but I need to add a class="thumbnail" to the link,
ie:
<a href="/games/:id" class="thumbnaill>
instead of what its currently generating:
<a href="/games/:id">
Thanks
Ref link_to
<%= link_to newGame, class: 'thumbnail' do %>
Be careful when using the older argument style, as an extra literal hash is needed:
<%= link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article" %>
#Gives Articles
You can just do
<%= link_to newGame, class: 'thumbnail' do %>
<%= image_tag newGame.image_url.to_s %>
<% end %>

Press Button and add to title

I have a post and it has a title. I want the user to be able to press a button and then after the title the word completed is placed in but I'm not sure how I would do this.
my post view looks like
<% div_for post do %>
<strong><%= link_to_unless_current h(post.title), post %></strong> - <%= link_to post.user.name, post.user %>
<%= simple_format h(post.body) %>
<% end %>
I feel like I may need an if statement saying if clicked original code for the view plus "completed" else original code but I'm not sure how I would use this logic in a button and how to describe this logic. I'm still a noob so I'm sorry if this question is overly simple.
What I want to do is have a button labeled completed that adds the work complete onto the end of the title.
this is my show view
<%= render :partial => #post %>
<% if current_user?(#post.user) %>
<p>
<%= link_to 'Edit', edit_post_path(#post)%>
<%= link_to 'Delete', #post, :method => :delete, :confirm => "Are you sure?" %>
</p>
<% else %>
<% end %>
<h2>Comments</h2>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<%= form_for [#post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br />
<%= f.text_area :body %>
</p>
<p><%= f.submit "Add Comment" %></p>
<% end %>
the view in shown in my main question is the view for each individual post so basically I want to add completed in front of <%= link_to_unless_current h(post.title), post %> that block of code when I press the button and then it should display the word completed and the original title
Im not getting your idea if what exactly shoul happen when clicking on the title, but if u want to change stuff on the fly in our html site, u need JS or ajax to handle this...
Could u please send more example code or specify what should happen or where the title should be placed?

How can I turn this into a block using a %w array in Ruby?

I have to have a series of buttons with different values:
<p>
<%= f.submit "Connected", :class => 'button' %>
<%= f.submit "Voicemail"%>
<%= f.submit "Hangup"%>
<%= f.submit "Not Interested" %>
<%= f.submit "Wrong Number" %>
</p`>
Looking at it it seems I could turn it into a do block and pass a %w array but don't know exactly how....? Thanks...
Example that illustrates the point :
%w[Connected Voicemail].each do |item|
<%= f.submit "#{item}" %>
end
Since this is Rails, don't put this directly in your view, but construct it in a helper.
you mean like this?
<% button_titles.each do |button_title| %>
<%= f.submit button_title, :class => 'button' %>
<% end %>

Resources