MKK
MKK

Reputation: 2753

Why this "if statement" won't work in partial?

In some cases, it doesn't have tag argument just like this below.
So I put <%= tag + ',' if tag %>, but it gets this error

ActionView::Template::Error (wrong number of arguments (0 for 1)):

How can I solve?

index.html.erb

<%= render 'layouts/social_like', :url => root_url, :title => @title %>

layouts/_social_like.html.erb

....
<a href="https://twitter.com/share" class="twitter-share-button" data-lang="en" data-hashtags="<%= tag + ',' if tag %>hash2,hash3,hash4">Tweet</a>
....

Upvotes: 0

Views: 91

Answers (2)

crazymykl
crazymykl

Reputation: 544

tag is a rails helper method that takes one arg; ruby thinks you are trying to call it without args. Rename your variable, and it should work.

Upvotes: 0

Zach Kemp
Zach Kemp

Reputation: 11904

Inline if statements don't really work well (at all?) in ERB. You can do this with a ternary operation:

...data-hashtags='<%= tag ? "#{tag}," : "" %>hash2,hash3,hash4'>

Upvotes: 1

Related Questions