slim dynamic conditional class [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just to help other developers, because there is no similar question on SO.
div class=(is_active? ? 'active' : 'inactive')
div class=('active' if is_active?)

See the examples below:
div class=(is_active? ? 'active' : 'inactive')
div class=('active' if is_active?)
The same approach can be used to assign dynamic values to other attributes.

I use array of classes and nil element if there is no need to include class in list, then compact array to remove nil elements and finally join all together.
div class=(["cday", "col-md-1", day.day == 1 ? "col-md-offset-#{day.cwday-1}" : nil].compact.join(' '))

If you have multiple conditions I am doing right now something like
div class=(('foo ' if is_foo?) + ('bar' if is_bar?))
Though I feel it to be a blemish if is_bar? return false and the generated HTML results in
<div class="foo "></div>
(the blemish is the blank character after the foo). If someone had a solution for that would be awesome.

Related

drawerOpened property dont work in vaadin [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I use litElement with vaadin and have next code
<vaadin-app-layout .drawerOpened='false'>
<vaadin-drawer-toggle slot="navbar" ></vaadin-drawer-toggle>
<vaadin-tabs selected="-100" slot="drawer" orientation="vertical" theme="minimal" style="margin: 0 auto; flex: 1;">
${this.renderTabsDependOnStatusAccess()}
</vaadin-tabs>
<div class="content" >
<div id="outlet"></div>
</div>
</vaadin-app-layout>
but my vaadin-drawer-toggle component is still open, how can I make it private by default and wats wrong with my example
I've not used this component, but I'll guess that in JS it's drawerOpened while in HTML it's drawer-opened. Also, boolean attributes are usually present or not, so instead of drawer-opened="false" you probably just remove it. Plain drawer-opened would be true.
If this HTML or in the LitElement render()? In the latter you may want to use ?drawer-opened="${this.isDrawerOpen}" and toggle a property this.isDrawerOpen

How to set up clickable boxes (Ruby) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to be able to set up kind of a navigatable menu. So like, there could be three boxes, each one with a number on it, and if you click on one of them it executes a certain code or takes one to another page. I feel dumb for not knowing how to do this, but I have success in everything but this, thanks so much!
It depends on what you want, but simply, you'd need this:
#config/routes.rb
resources :pages
#app/views/elements/nav.html.erb
<% pages = %w(home about contact) %>
<% for page in pages do %>
<%= link_to "Home", page_path(page) %>
<% end %>
You'll be able to style the boxes to look like buttons using CSS

When should I use helpers? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I barely use them in my projects. Is this normal? If I need to return just one line of html, say an image tag for a gravatar, then a helper is great:
def gravatar_for(user, height=90, width=90, alt=user.name + "'s gravtatar")
gravatar_address = 'http://1.gravatar.com/avatar/'
clean_email = user.email.strip.downcase
hash = Digest::MD5.hexdigest(clean_email)
image_tag gravatar_address + hash, height: height, width: width, alt: alt
end
However, if I want to loop through some errors, and return html structure rather than a single line, it quickly gets difficult.
Is a good rule of thumb if you want to return one line of html then use a helper, else use a partial to return more complex html?
First of all, the question is opinion based. So, most likely it will be closed.
Here are several questions on similar subject:
DRYing rails view: partial vs helper
Why shouldn't Helpers have html in them?
Generally speaking, helper is just a way to
Have DRY views
Make clean views and controllers
Generally speaking you can DRY views by moving out some duplicated functionality to controllers. However, as result controllers got cluttered. So, the better way to keep view related helpers in helpers (vs controllers), which you already figure out.
Some Helpers don't touch HTML at all (as example, they may just provide some formatting of numbers etc) or they may generate just one tag (as in your example) or generate piece of HTML code. However, as soon as you are talking about some piece of HTML code, you rather use partials vs helper.

Retaining values even after form submit in ROR [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to create a simple calculator application .
calculator.html.erb
<input type=text name="operand_one" value="0"></td>
<input type=text name="operand_two" value="0"></td>
.
.
.
<input type=text name="output" value="<% =#result %>"></td>
I am able to get the output correctly but the values of the first two text boxes changes to 0 , I need to know how to retain these values ..
You're hardcoding the value to "0" with this part of the code
value="0"
you could change the lines to
<input type=text name="operand_one" value="#value_one"></td> #etc...
and in your controller
#value_one = 0 unless X
but X depends on how your controller works - do you go to a new action to solve the problem in the calculator? if so you'll have to use the params hash.
Not related to the question, but why are you using text for your calculation fields? and are you hard coding that html or it's just the output?

rails how to declare a variable and store a value in html.erb [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
hi i have a value like this in html.erb
<img src="<%= file.title, file_path(file) %>" alt=""/>
but when I placed the above code it throws an error
SyntaxError in Files#index
the error comes on file_path(file) section . Is there a way to accomplish this ? like declare a variable and assign it to something ?
Though I did not get whay you are trying to achieve but you can try to do it like this
<img src="<%= "#{file.title}, #{file_path(file)}" %>" alt=""/>
You should declare file (a better naming is #file) in the controller action which is associated with this view. There you can do whatever you want with your variable.

Resources