New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link_to with block in slim doesn't seem to work #777

Closed
simonc opened this Issue Feb 5, 2012 · 28 comments

Comments

Projects
None yet
@simonc

simonc commented Feb 5, 2012

I am using slim as my template syntax and using a block with the link_to helper doesn't work.

My test is like this:

== link_to("/") do
  span Hello

It doesn't print anything.

I digged into the code and traced it back to SlimHandler.concat_to_template:

def concat_to_template(text="")
  self.output_buffer << text if is_type? && text
  nil
end

It doesn't come from the condition, I commented it and it didn't change anything.

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Feb 6, 2012

Member

Interesting, thanks for bringing this up. Hopefully can investigate soon.

Member

nesquena commented Feb 6, 2012

Interesting, thanks for bringing this up. Hopefully can investigate soon.

@mitya

This comment has been minimized.

Show comment
Hide comment
@mitya

mitya Feb 7, 2012

It seems that all the block helpers that use capture_html are broken. And the helpers that use concat_content don't output anything to the view. So things like form_for and form_tag are not working also.

mitya commented Feb 7, 2012

It seems that all the block helpers that use capture_html are broken. And the helpers that use concat_content don't output anything to the view. So things like form_for and form_tag are not working also.

@mitya

This comment has been minimized.

Show comment
Hide comment
@mitya

mitya Feb 7, 2012

Also capture_html works in a different way inside the - and = blocks. When used from - helpers it returns something senseless (like the entire template content), while for = and == helpers it works correctly, but returns and array instead of a string.

mitya commented Feb 7, 2012

Also capture_html works in a different way inside the - and = blocks. When used from - helpers it returns something senseless (like the entire template content), while for = and == helpers it works correctly, but returns and array instead of a string.

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Mar 14, 2012

Member

For reference this all stems from: https://github.com/padrino/padrino-framework/blob/master/padrino-helpers/test/test_output_helpers.rb#L104 it's been a problem for a while as far as I understand.

Member

nesquena commented Mar 14, 2012

For reference this all stems from: https://github.com/padrino/padrino-framework/blob/master/padrino-helpers/test/test_output_helpers.rb#L104 it's been a problem for a while as far as I understand.

@Cirex

This comment has been minimized.

Show comment
Hide comment
@Cirex

Cirex Mar 29, 2012

I was bitten by this awhile ago. I have a few other changes, but I think this was all thats needed:

module Padrino
  module Helpers
    module OutputHelpers
      class SlimHandler < AbstractHandler
        def capture_from_template(*args, &block)
          buffer = ''
          old_buffer, @_out_buf = @_out_buf, buffer
          yield(*args)
          buffer
        ensure
          self.output_buffer = old_buffer
        end
      end
    end
  end
end

Cirex commented Mar 29, 2012

I was bitten by this awhile ago. I have a few other changes, but I think this was all thats needed:

module Padrino
  module Helpers
    module OutputHelpers
      class SlimHandler < AbstractHandler
        def capture_from_template(*args, &block)
          buffer = ''
          old_buffer, @_out_buf = @_out_buf, buffer
          yield(*args)
          buffer
        ensure
          self.output_buffer = old_buffer
        end
      end
    end
  end
end
@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Mar 29, 2012

Member

Cool, thanks. I will play with tweaking that and see if I can get the helpers working better in slim

Member

nesquena commented Mar 29, 2012

Cool, thanks. I will play with tweaking that and see if I can get the helpers working better in slim

@DAddYE

This comment has been minimized.

Show comment
Hide comment
@DAddYE

DAddYE Apr 24, 2012

Member

@nesquena can you take a look a this? I want to close it for next release.

Member

DAddYE commented Apr 24, 2012

@nesquena can you take a look a this? I want to close it for next release.

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Apr 24, 2012

Member

Yep I will fix this week.

Member

nesquena commented Apr 24, 2012

Yep I will fix this week.

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Apr 27, 2012

Member

I think I got this working somewhat here 8a670de and 8b79d57. Can you take a look @Cirex I applied your patch. @DAddYE

Member

nesquena commented Apr 27, 2012

I think I got this working somewhat here 8a670de and 8b79d57. Can you take a look @Cirex I applied your patch. @DAddYE

@WaYdotNET

This comment has been minimized.

Show comment
Hide comment
@WaYdotNET

WaYdotNET Apr 27, 2012

Contributor

ERROR:
with this change, i've ALWAYS this error

- form_tag "xx" do
undefined method `<<' for nil:NilClass
Contributor

WaYdotNET commented Apr 27, 2012

ERROR:
with this change, i've ALWAYS this error

- form_tag "xx" do
undefined method `<<' for nil:NilClass
@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Apr 27, 2012

Member

@WaYdotNET are you on slim? Out of curiosity, what happens if you do

== form_tag "xxx" do

instead

Member

nesquena commented Apr 27, 2012

@WaYdotNET are you on slim? Out of curiosity, what happens if you do

== form_tag "xxx" do

instead

@WaYdotNET

This comment has been minimized.

Show comment
Hide comment
@WaYdotNET

WaYdotNET Apr 27, 2012

Contributor

some error

i'm using slim

Contributor

WaYdotNET commented Apr 27, 2012

some error

i'm using slim

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Apr 27, 2012

Member

Hmm that's sad, ok I'll take another look and add some tests

Member

nesquena commented Apr 27, 2012

Hmm that's sad, ok I'll take another look and add some tests

WaYdotNET added a commit to WaYdotNET/padrino-framework that referenced this issue Apr 27, 2012

@chevalun

This comment has been minimized.

Show comment
Hide comment
@chevalun

chevalun May 20, 2012

Plz, fix this.

chevalun commented May 20, 2012

Plz, fix this.

@DAddYE

This comment has been minimized.

Show comment
Hide comment
@DAddYE

DAddYE Jun 11, 2012

Member

Im working on it.

Member

DAddYE commented Jun 11, 2012

Im working on it.

@DAddYE

This comment has been minimized.

Show comment
Hide comment
@DAddYE

DAddYE Jun 11, 2012

Member

After investigate on it I can confirm problem with @WaYdotNET and Im unable to reproduce problem @simonc and @chevalun . Maybe latest slim 1.2.1 has fixed it? Can you guys update all your gems and retest it?

We have also this: https://github.com/padrino/padrino-framework/blob/master/padrino-helpers/test/fixtures/markup_app/views/link_to.slim#L3

Which pass on all rubies.

Since we have some false positive tests (@nesquena can you help me to figure why?), I've tried it in a real app, and it works perfectly:

== link_to '/' do
  span Hello World

Generates correctly:

<a href="/"><span>Hello World</span></a>
Member

DAddYE commented Jun 11, 2012

After investigate on it I can confirm problem with @WaYdotNET and Im unable to reproduce problem @simonc and @chevalun . Maybe latest slim 1.2.1 has fixed it? Can you guys update all your gems and retest it?

We have also this: https://github.com/padrino/padrino-framework/blob/master/padrino-helpers/test/fixtures/markup_app/views/link_to.slim#L3

Which pass on all rubies.

Since we have some false positive tests (@nesquena can you help me to figure why?), I've tried it in a real app, and it works perfectly:

== link_to '/' do
  span Hello World

Generates correctly:

<a href="/"><span>Hello World</span></a>

DAddYE added a commit that referenced this issue Jun 11, 2012

@nesquena

This comment has been minimized.

Show comment
Hide comment
@nesquena

nesquena Jun 11, 2012

Member

Thanks for checking into it @DAddYE

Member

nesquena commented Jun 11, 2012

Thanks for checking into it @DAddYE

@DAddYE DAddYE closed this Aug 7, 2012

@akiva

This comment has been minimized.

Show comment
Hide comment
@akiva

akiva Jun 13, 2013

This problem is still occurring for me. It seems the issue was closed in reference to revert a bad fix, rather than actually resolving the issue?

akiva commented Jun 13, 2013

This problem is still occurring for me. It seems the issue was closed in reference to revert a bad fix, rather than actually resolving the issue?

@pencilcheck

This comment has been minimized.

Show comment
Hide comment
@pencilcheck

pencilcheck Jun 30, 2013

Still doesn't work for me here either.

pencilcheck commented Jun 30, 2013

Still doesn't work for me here either.

@dariocravero

This comment has been minimized.

Show comment
Hide comment
@dariocravero

dariocravero Jul 2, 2013

Contributor

@akiva @pencilcheck the simple example mentioned by @simonc and @DAddYE is working fine for me too with 0.11.2 and master. Can you provide a gist showcasing your issues? Thanks!

Contributor

dariocravero commented Jul 2, 2013

@akiva @pencilcheck the simple example mentioned by @simonc and @DAddYE is working fine for me too with 0.11.2 and master. Can you provide a gist showcasing your issues? Thanks!

@pencilcheck

This comment has been minimized.

Show comment
Hide comment
@pencilcheck

pencilcheck Jul 3, 2013

= link_to 'test', root_path do
    span Hey

is not working for me

pencilcheck commented Jul 3, 2013

= link_to 'test', root_path do
    span Hey

is not working for me

@akiva

This comment has been minimized.

Show comment
Hide comment
@akiva

akiva Jul 3, 2013

I concur with Penn. :/

akiva commented Jul 3, 2013

I concur with Penn. :/

@dariocravero

This comment has been minimized.

Show comment
Hide comment
@dariocravero

dariocravero Jul 4, 2013

Contributor

That seems to be working fine for me too (= instead of ==). Quick not: root_path won't be shown, see this.

Despite that, the link should have a span inside.

Are you able to reproduce this on a new application? Can you gist it?
Thanks!

Contributor

dariocravero commented Jul 4, 2013

That seems to be working fine for me too (= instead of ==). Quick not: root_path won't be shown, see this.

Despite that, the link should have a span inside.

Are you able to reproduce this on a new application? Can you gist it?
Thanks!

@akiva

This comment has been minimized.

Show comment
Hide comment
@akiva

akiva Jul 4, 2013

@dariocravero I will try it out again and get back to you shortly. Can't at the moment, sorry.

akiva commented Jul 4, 2013

@dariocravero I will try it out again and get back to you shortly. Can't at the moment, sorry.

@rafaelgonzalez

This comment has been minimized.

Show comment
Hide comment
@rafaelgonzalez

rafaelgonzalez May 5, 2015

@pencilcheck and @akiva

Not sure if you figured this out by now, but I was trying to do something like:

li
  = link_to channel.name, channel_path(channel) do
    figure
      img src="//placekitten.com/g/400/200"
    h3
      = channel.name
    p
      = channel.description

This didn't work. This is because I'm trying to set the content of the link with the first argument of link_to, when of course the content of the block is doing that.

Once you remove that first argument: = link_to channel_path(channel) do it works!

rafaelgonzalez commented May 5, 2015

@pencilcheck and @akiva

Not sure if you figured this out by now, but I was trying to do something like:

li
  = link_to channel.name, channel_path(channel) do
    figure
      img src="//placekitten.com/g/400/200"
    h3
      = channel.name
    p
      = channel.description

This didn't work. This is because I'm trying to set the content of the link with the first argument of link_to, when of course the content of the block is doing that.

Once you remove that first argument: = link_to channel_path(channel) do it works!

@jarthod

This comment has been minimized.

Show comment
Hide comment
@jarthod

jarthod May 15, 2015

Not sure if it helps but I recently switched to slim and got a similar issue, where:

= link_to new_user_session_path do
  | sign in

Would render the whole page template again instead of just the link and found the reason to be streaming. I'm using render stream: true and if I simply render the page without streaming the link_to with block is working as expected.

With streaming enabled it seems slim renders the block first (I got the content of the link outside, before it) and then renders the link with the whole template inside. So you may want to have a look at this in stream mode ;)

jarthod commented May 15, 2015

Not sure if it helps but I recently switched to slim and got a similar issue, where:

= link_to new_user_session_path do
  | sign in

Would render the whole page template again instead of just the link and found the reason to be streaming. I'm using render stream: true and if I simply render the page without streaming the link_to with block is working as expected.

With streaming enabled it seems slim renders the block first (I got the content of the link outside, before it) and then renders the link with the whole template inside. So you may want to have a look at this in stream mode ;)

@kunzig940

This comment has been minimized.

Show comment
Hide comment
@kunzig940

kunzig940 Mar 6, 2017

I also cannot get link_to blocks such as
= link_to some_path_here do
| Some html here
To work. Inline links such as: link_to "LinkName", some_path, data: {whatever: 'works'} work!

kunzig940 commented Mar 6, 2017

I also cannot get link_to blocks such as
= link_to some_path_here do
| Some html here
To work. Inline links such as: link_to "LinkName", some_path, data: {whatever: 'works'} work!

@canjoku

This comment has been minimized.

Show comment
Hide comment
@canjoku

canjoku May 4, 2018

you can pass content as a block [in this case Home. It does'nt have to be string either]

<%=  link_to(route_path(:id)) do %>
    'Home'
<% end %>
  Outputs => <a href="/">Home</a>

canjoku commented May 4, 2018

you can pass content as a block [in this case Home. It does'nt have to be string either]

<%=  link_to(route_path(:id)) do %>
    'Home'
<% end %>
  Outputs => <a href="/">Home</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment