joining two tables with multiple conditions - postgresql

I have following code in my template
<% #dishes.each do |d| %>
<div class="card">
<img src="<%= d.image_url %>" alt="">
<p><%= d.name %></p>
<p><%= d.dish_type.name if d.dish_type %></p>
<h4># <%= d.venue %></h4>
<form action="/likes/<%= d.id %>" method="post">
<button <%= 'disabled' if d.likes.where(user_id: current_user.id) %>>Like</button>
<p>Count: <%= d.likes.count %></p>
</form>
What I am trying to achieve is when a user clicks on 'like' button I should disable the button.
I have three tables dishes, users, likes. likes table contains dish_id and user_id as foreign keys. You can say many to many relationship where likes is a junction table.
I am activerecord I have setup all the connections and belongs_to and has_many relations, they all are good.
The problem I am facing is at this particular line
<button <%= 'disabled' if d.likes.where(user_id: current_user.id) %>>Like</button>
somehow the join condition is not working, it always evaluates to true. I tried different variations like using 'joins' and all but nothing is working.
Any help is appreciated. Please let me know if further information is required?

I think it always evaluates to true because the where clause is returning an active record relationship object.
If you want to get true or false from that particular query you can use "exist?":
d.likes.exists?(user_id: current_user.id)
If the record exists in the database you will get True otherwise False.

Related

ejs file. Creating a card to show articles "%> <%"

I am following a youtube tutorial on creating a blog using node, MongoDB, and express.
I am having difficulty understanding some stuff.
<!--Creating the Article Cards-->
<% article.forEach(article =>{ %>
<div class="card mt-4"><%= article.title%></div>
<% }) %>
here is the code
%>
<div class="card mt-4"><%= article.title%></div>
<%
the area I don't understand is why there is and what " %> <% " mean as you can see above after doing the forEach.
Well, that is a scriplet tag that is used for the control-flow you need to use in the if-else as well. Here is more for the ejs ejs

Displaying Rails Required Fields Form Errors All At Once

I'm using the Rails form and I'm also using Bootstrap tabs. I have 4 tabs that a user fills out and on the 4th tab is the submission button. I have a number of required: true fields in the form, and when I go to click on the submit button leaving any of these fields blank, the pop-up saying "Please fill out this field" appears.
Question: How can I just have a box appear with ALL the error messages, instead of each individual error message appearing one at a time?
I've read a number of posts, and have tried most suggestions (with exception to ones that include JS, as I'm hoping there is a strong solution not including JS). I put below some code below my submit button that I tried but it doesn't display anything as the individual box error message I think overrides it.
Any help is appreciated.
_Form:
<%= form_for(#property, html: { multipart: true }) do |p| %>
...
<%= p.file_field :picture, :multiple => true, name: "property_attachments[picture][]", size: 2 %>
<%= p.submit "Submit", class: 'btn btn-primary' %>
<% if #property.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#property.errors.count, "error") %>.
</div>
<ul>
<% #property.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
Figured it out and learning a lot about Rails Forms. I was using required: true for a number of fields and this created individual pop-ups on each error one by one. I removed all of the required: true fields and then added validations in the model (eg. validates: :price, presence: true) and then the error message I used above in my question displayed any missing fields.

How to get the name of the current user in Refinery?

In RefineryCMS, for some functionality I have to store the present logged in user's first name and last name.
How to get logged in user's first name and last name?
While going through this implementation, I found that refinery user table does not have column to store first name, last name or any basic details about user.
So, first step is to create a migration file having first_name, last_name etc as per requirement.
$ rails generate migration AddFirstNameLastNameToRefineryUsers
first_name:string last_name:string
Then run
$ rake db:migrate
Now, create a decorators for user model.
Path :: /app/decorators/models/refinery/user_decorator.rb
Create a decorator : Refinery Documentation Link for Creating Decorator
Add this line inside that model
attr_accessible :first_name, :last_name
validates :first_name,:last_name, :presence => true
Now add this line at view area
/app/views/users/new.html.erb
<div class='field'>
<%= f.label :first_name %>
<%= f.text_field :first_name, :class => 'larger widest' %>
</div>
<div class='field'>
<%= f.label :last_name %>
<%= f.text_field :last_name, :class => 'larger widest' %>
</div>
Now, Rails will save the values, when user makes first time login.

select and onChange in a Ruby on Rails form

I browsed all SO questions and answers about this topic but I'm still unable to make my scenario work.
I want to trigger a click button action when a dropdown menu option is selected ; seems simple and should be very common with AJAX.
Here are the relevant excerpts of my code:
<%= form_for(#test, :html => {:id => "form_id", :name => "MyForm", :remote => "true"}) do |form| %>
<%= form.label "Menu1" %>
<%= form.select (:Menu1, [["Option1","value1"],["Option2","value2"]], :html_options=>{:onChange=>"javascript: this.form.apply_button_name.click();"}) %>
<!-- more select menus and text fields here -->
<div class="actions">
<%= form.submit "Apply", :name => "apply_button_name", :remote => "true" %>
</div>
<% end %>
I used ":remote => "true" both for the form and the button because that's the only way to get AJAX working. I also tried with and without explicit "html_options" and "javascript:", after I browsed some SO answers that suggested that but that did not help. I also tried onSelect, and onClick instead of onChange, but still no luck.
The generated HTML is the following:
Menu1
<select id="test_Menu1" name="test[Menu1]"><option value="value1">Option1</option>
<option value="value2" selected="selected">Option2</option></select>
As you can see, there's no onChange event handler in the HTML code ; WHY? Anyone is seeing what am I doing wrong?
Thanks for any help.
Modify your call to form.select, like this:
<%= form.select :Menu1, [["Option1","value1"],["Option2","value2"]], {},
:onChange=>"javascript: this.form.apply_button_name.click();" %>
If you examine the documentation for:
API Dock Ruby on Rails select
You will see that the select form helper takes the form:
select(object, method, choices, options = {}, html_options = {})
If you don't pass anything for the option hash (in your case this will be an empty hash), the form thinks that your html_options hash are your options hash, and gets confused.
A way to check this is to add something like {:onchange=> "alert('Hello');"} and either see if the event successfully triggers, or alternatively, in your actual web page, right click on the select element and inspect it. If no onchange option is present in the html, that means that your rails form helper is indeed confusing the html_options with the other options. So, what you should have:
<%= form.select (:Menu1, [["Option1","value1"],["Option2","value2"]], {}, {:onChange=>"handler();"} %>
MAKE SURE TO INCLUDE THE EMPTY HASH FOR THE OPTIONS BEFORE THE HTML OPTIONS AND YOU SHOULD BE FINE. I don't think you even need to have the html_options and javascript stuff you have.
Lastly, if onChange doesn't work, try to use onchange with no capital C.

How do I change the text_area default size for a form in rails 3?

I'm going through Agile Web Development with Rails and I'm having some trouble with the form helper text_area. Specifically, I want to make the text area smaller (the form submits correctly and everything goes into the database correctly). According to the book this code should work:
<%= form_for(#request) do |f| %>
<div class="actions">
...
<div class="field">
<%= f.label :quote_details, "*Items required:" %>
<%= f.text_area :quote_details, :rows=>5, :cols=>40 %>
</div>
It seems that no matter what numbers I put for :rows or :cols, the box stays the same default size. Instead of :rows and :cols, I used :size=>"3x40" and size=>"5x8" etc.. but the box still always stays the same size.
As an experiment I tried
<%= f.text_field :quote_details, :size=>"300*39" %>
That changed the number of columns, but removing the :size and putting :rows or :cols has no effect (it goes back to a default size for a text_field).
I did see this:
Change default Rails text_area helper rows/cols
I tried answer 1, but the answer given didn't work for me. I don't really understand what the second and third answers mean. I might be doing something else wrong or maybe it's a different problem.
I'm just stumped. Any help or ideas on what's going on would be greatly appreciated. Thanks for any responses.
Oh, I'm using rails version 3.0.0 and ruby 1.9.2p0 on vista.
your first code segment has :cols => 40% instead of 40?
I would also consider using CSS to do it, as that can make changing the look of the webpage isolated to the CSS presentation layer.
try do it with form_with. For me it works.
<%=form_with, local: true do |form| %>
<%= form.label :comment %>
<%= form.text_area :body, :rows=>10, :cols=>60 %>
<% end %>