I have some images. When an image is clicked I want to get that image displayed in a different div. Been struggling with how to solve this. This is what I've got so far.
<% #user.images.each do |image| %>
<%= link_to image_tag((image.url), :id => 'test')%>
<% end %>
js.coffee
$('#test').click ->
val = $(this).attr("src")
alert val // /assets/image.png
Firstly, since you have an array of images, you should use class than id.
<% #user.images.each do |image| %>
<%= link_to image_tag((image.url), :class => 'test')%>
<% end %>
And in JS, you will bind click event to image with class 'test'.
$(".test").click ->
val = $(this).attr("src")
$("#showDiv").html "<img src=\"" + val + "\" />"
Where #showDiv is the id of the div which you want the image to be shown.
Related
My scenario is on the home page I want to click a dropdown toggle/select. In that toggle I want it to display a list of Location objects. However I want to pass individual params that are different in each one. My goal is to use a dropdown to toggle location information in the view. The problem is right now I'm hard coding locations into this drop down. But I am creating Location objects on a different page. I want those Location objects to populate the dropdown on the home page, but allow the user's choice to pass params to change the view. This would allow an unlimited number of locations. What is a good way to accomplish this.
index.html.erb
**I erased the dropdown code to simplify things.**
<li><%= link_to "#{#location1}", {:location1 => "location1"}%></li>
<li><%= link_to "#{#location2}", {:location2=> "location2"}%></li>
home_controller.rb
if params[:location1]
#current_location = Location.last
#myreviews = Review.where("location_id = ?", #current_location.id).order('created_at asc')
end
if params[:location2]
#current_location = Location.first
#myreviews = Review.where("location_id = ?", #current_location.id).order('created_at asc')
end
SOLUTION
Here is what I did to solve the problem in the end.
View
<ul class="dropdown-menu loc-drop scrollable-menu" role="menu" aria-labelledby="menu1">
<% #locations.each do |u| %>
<li><%= link_to u.name, :params => {:loc => u.id} %></li>
<% end %>
</ul>
</ul>
Controller
#locations = Location.all
if params[:loc]
#chosen_location = Location.where('id = ?', params[:loc]).pluck(:id).first
end
At that point I'm using #chosen_location to display details like name and address.
i have this form nested in view 'A'
<div>
<%if current_user%>
<%=form_for(ModelB.new) do |f|%>
<%if (params[:param_to_check].present?)%>
<%f.hidden_field "param1",:value=> #modelA.id%>
<%f.hidden_field "param2",:value=> current_user.id%>
<%end%>
render button
<%end%>
<%else%>
render other butotn
<%end%>
</div>
and this 'find' in Bs_controller:
#modelA=ModelA.find_by_id(params[:param1])
:option_from_specific_gem=> -(#modelA.wanted_value).abs
when i look to the params passed to the Bs_controller i see:
Parameters: {"authenticity_token"=>"some_hash=", "utf8"=>"✓", "y"=>"42", "x"=>"144"}
and i need to pass the hidden_field's valies to Bs_controller.
thx for the atention :)
here is the gist: https://gist.github.com/rmatuoka/dd6904809cf89cf916f7
I made some comments, I'm gonna go ahead and make them into an answer.
There are 3 possible things. Assuming it's not a typo, you aren't currently outputting the hidden fields to the HTML. You need to us <%= %> to output the result.
<%= f.hidden_field "param1",:value=> #modelA.id %>
<%= f.hidden_field "param2",:value=> current_user.id %>
This is likely the issue. Otherwise I'd guess either current_user or params[:param_to_check] is nil.
Add param1 and param2 to your list of permitted parameters. In your controller, you should have something like:
def modela_params
params.require(:modela).permit(:x, :y, :param1, :param2)
end
If I want to insert a <br/> as part of the markup inside the label form helper, how do I do it?
Right now when I do this:
<%= f.label(:foo, "Foo <br/> Bar: ") %>
It outputs Foo <br/> Bar as the text of the label.
What I want is the label to have the line break between Foo and Bar. Like this:
Foo
Bar
The string needs to be marked as HTML safe so that Rails will not sanitize the output. Here is the concise way to write the label.
<%= f.label :foo, 'Foo <br /> Bar: '.html_safe %>
Use the raw method
<%= f.label(:foo, raw("Foo <br/> Bar: ")) %>
I'm a real beginner with MongoDB and MongoID.
I created two scaffolds
class Objet
include Mongoid::Document
field :nom, type: String
embeds_one :coordonnee
end
And
class Coordonnee
include Mongoid::Document
field :adresse1, type: String
field :adresse2, type: String
field :code_postal, type: String
field :ville, type: String
embedded_in :objet
end
That's what I get when creating a new Objet :
Now, I'm trying to show only the field adresse1 for this document, but it doesn't work. I can display only the whole embedded document doing this :
When I do :
<%= #objet.coordonnees.adresse1 %>
I get this error :
undefined method `adresse1' for #<Hash:0x2b2b1f0>
How can I do that ?
EDIT
Doing that, I can display all the elements "Adresse1, adresse2, ville, code_postal" :
Controller
def show
#objet = Objet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #objet }
end
end
View
<%= #objet.nom %>
<% #objet.coordonnee.each do |t| %>
<%= t[1] %>
<% end %>
But my question is : How to display ONLY one of them ? Such as ville, or code_postal or adresse1... ?
What was your code that works for the full document? It was dropped from your post.
In the mongo Shell, you could do this with dot notation db.collection.find({},{'coordonnees.adresse1':1,'_id':0}) You need to specify the '_id':0 because _id is always returned by default.
The other answer will not work because adresse1 is a subdocument. You must include the reference to coordonnees.
Not hugely familiar with MongoID, but assuming you can make calls straight to mongo, there is a second implicit parameter to all find-like statements called a projection that specifies what exactly you would like to return.
For instance, showing only adresse1 for all items in your collection:
db.collection.find({},{"coordonnees.adresse1": 1, "_id":0})
should return only the adresse1 parameter. I wasn't quite able to tell exactly what context you're displaying the objects in, but regardless of context, api calls to mongo should be fairly straightforward to make. Let me know if I've misinterpreted this question though.
In your posted example, you should change your find function to something like the following:
Objet.find({params[:id]}, {:fields => [coordonnees.adresse1]})
Hope that helps.
I found the solution to my problem.
To display only one element of the hash, I can do :
<%= #objet.coordonnees['adresse1'] %>
I am not sure if you are using embeds_one or embeds_many as you are using singular and plural forms of the relation name interchangeably in your question.
If it is a embeds_one the problem is that you should not iterate on #objet.coordonnee as it is a single document. Your view code should look like:
<%= #objet.nom %>
<%= #objet.coordonnee.address1 %>
If it is a embeds_many, your relation name should be plural, then you should be able to use t.address1 in your view.
# model Objet
embeds_many :coordonnees
# view
<%= #objet.nom %>
<% #objet.coordonnees.each do |t| %>
<%= t.address1 %>
<% end %>
I writting a simple app in that an item can have multiple line items. When you go into the items detail view you can see all the line items that belong to it.
I'd like to add a line number to the line_items, when I'm in the item detail view I'd like to see
line_item_1
line_item_1
line_item_1
I was thinking about an autoincrement columns, but this would start from 1 when I reimport the data. So maybe a virtual column, but how would I calculate what to display?
Any ideas?
Thanks
Thomas
You could do:
<% line_items.each_with_index do |item, index| %>
<%= render :item_partial, :locals => {:item => item, :index => index} %>
<% end %>
item partial:
<%= index %>. <%= item.name %>
If you want to keep the same order every time, just order your line items by creation date.