Flexible image buttons in HAML - ruby-on-rails

I'm working on a Rails application and using HAML for the views. I would like to use the "sliding doors" technique to create pretty buttons, which means I need to get HAML to generate something similar to the following HTML:
<span class="button">Button text</span>
I haven't been able to figure out how to do that from the HAML reference or Google. Could you help please? Many thanks in advance.
EDIT: To clarify, I need to use the link_to helper because I'm linking to a resource. When I try to pass a block into the method, I get an error: undefined method 'stringify keys'

Try
= link_to invitation_path(invitation), :method=>:put, :class=>"button" do
%span(class="button")
Accept

This should be as simple as:
%a(href="#" class="button")
%span(class="button") Button text
Or if you especially want it on one line without any whitespace you can do:
%a(href="#" class="button")<
%span(class="button") Button text

Related

Render images into my post content

I'm trying to add images to my post which I thought that would be easier but god I've been struggling for a while now.
I tried to use Redcarpet and added some ruby Image_tag code but didn't work. Also I tried with tag and the raw method but no luck. I don't know I've been going around but I couldn't fine anything and I think I'm not asking nothing weird or complicated ahah.
Just add pictures from my /assets/images folder if is possible.
<%= img_tag ("my_photo.jpg")%>
How do you recommend to do it?
Many thanks
I use the image_tag helper and this has always worked fine for me. You could try this:
<%= image_tag("my_photo.jpg")%>
I figured it out. I used HTML tag instead of Ruby code.
<%= raw (#post.content) %>
As I am using assets pipeline:
<img src="/assets/my_photo.jpg" alt="twd" class="img-rounded"></img>
My issue was that I was calling my photo as /images/my_photo.jpg instead of /assets/my_photo.jpg... simple as that

Button to an external link or styling a link tag to look like a button in Ruby on Rails

I'm a newbie to RoR, but I'm looking to create a button that takes users to an external URL.
I've had trouble using link_to, because I haven't been able to style the hyperlink look like a button.
Then, I am able to get the button to appear with the following code:
%a#about-join-our-team.button Current Open Positions
But I can't add a URL to it in this format. (Button text = "Current Open Positions")
I'm sure I'm making some silly RoR mistake, but I just can't find the right answer in previous threads.
Thanks for you help!
ROR actually has a built-in helper called button_to which does exactly what you need:
= button_to "Current Open Positions", your_url_path, class: "class"
This basically creates a small form, which Rails uses to send a request to. The user sees a standard HTML button
Following should work(HAML)
%a#about-join-our-team.button{href: your_url} Current Open Positions
(erb)
<a class="button" href="<%= your_url %>" id="about-join-our-team">Current Open Positions</a>

Is it a good idea to create a helper method for this type of scenario?

I have this code in my html.erb at many places.
<div id="left-nav">
<%= render :partial => 'tests/tests_left_menu' %>
</div>
Is it a good idea to create helper method for this type of code ?
How to write this code in helper ?
I see a few good strategies to use in your situation. Pick and choose based on your project's specific requirements.
You can just put div#left-nav and its contents into yet another partial like tests/tests_left_menu_with_wrapper. This saves you a couple of lines.
If you can generalize the cases when the entire segment appears, you can move it into a layout. This way, once you declare the layout for a particular action using the ActionController::Base.layout method, you'll be able to skip writing the entire segment altogether.
You can write a helper, but it's not clear what advantage it confers over simply using content_tag. You're probably better off using partials or layouts.
Personally i don't think there's a need to, and i think it's more like because you are not using other tools like haml to help reduce the number of lines in an erb files
the same code can be achieved in haml in just 1 line:
#left-nav= render :partial => 'tests/tests_left_menu'
hope this helps =)
I suppose if you have that code in many places I'd move the the div into the partial. If you need the flexibility to have tests_left_menu outside of the div I'd still pick two partials over a helper in this scenario. Avoid writing html in Ruby when you can :)

Rails Link_To doesn't add a CSS Class correctly

So I'm trying to use link_to to create a link in my Rails app and trying to add a CSS class to certain links. The problem that I have is that when I add my html options to the link_to arguments, the links don't get created and I end up with nothing. Here's my code:
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :html=>{:class=>"work"}) %>
I also tried variations of this and it still didn't work. For example,
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :class=>"work") %>
The method also exhibits some strange behavior, which I think might be the culprit. If I go straight to the page, no POST or GET requests, link_to works properly and the links and images render correctly, which is to say that they actually DO render. However, the way that I would like to get to the page is by form POST request in a previous page whose action is the results page I'm trying to get to.
Thanks for any help you can provide!
EDIT: I'm not sure exactly what the problem was, but I solved it by changing the form's method to GET instead of POST.
It's most likely because the POST request is hitting a different method (new), rather than (show). You must supply the proper instance variables to the view. You seem to be referencing both #beetle and beetle. Have a look at all of those variables, since that appears to be the POST problem.

Rails: Creating an HTML Snippet with a Variable?

I have a submit button that is a block of HTML code because of styling and images to make it look better. (I stole most of it from Wufoo).
This is one every form in the application and I was wondering if there is a cleaner way to do this. Something like a partial or helper?
The name of the button "Submit" or "Add Contact" needs to be a variable.
snippet
Add Contact #variable text
Back
* Required
You can use a application-wide helper for this. Helpers are modules containing methods that are shared by multiple views. You can easily define your own helper that works like the 'submit_tag' helper method that generates the button.
consider partials (http://api.rubyonrails.org/classes/ActionView/Partials.html)
Seems like something partials were invented for.

Resources