use an image for radio button label rails 4

我只是一个虾纸丫 提交于 2019-12-22 10:52:40

问题


I am trying to use the helper collection_radio_buttons to show an image as a label, and want to save the image_id to the model so that I can retrieve the url, so far i have come up with this which shows a radio button and the image_url as the label

Something i have noticed already is that i can only click the radio button once and it then just stays in its on state

<%= f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) %>

The html generated looks like so

<input id="campaign_default_image_id_1" name="campaign[default_image_id]" type="radio" value="1" />
<label for="campaign_default_image_id_1">https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/1/vandals_portfolio.png</label>

How do i set this up correctly? can i wrap the label in a image_tag ?

Any help appreciated

EDIT

So after some more googling and trying to piece this together i can now render an image and a radio button, but the image is the default image i uploaded with carrierwave and not the resized :small version that i would like..can i pass the :small version through?

<%= f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b| %>
<%= b.label { image_tag("#{b.text}") + b.radio_button } %>

b.text retrieves the url

https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/2/ymca_portfolio.png

but i would like it to prefix the filename with fancy_box_array, like so

https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/2/fancy_box_array_ymca_portfolio.png

the html generated with this new code is

<label for="campaign_default_image_id_1"><img alt="Vandals portfolio" src="https://calcuttatest.s3.amazonaws.com/uploads/default_image/image/1/vandals_portfolio.png" />
<input id="campaign_default_image_id_1" name="campaign[default_image_id]" type="radio" value="1" /></label>

thanks


回答1:


There is a good reference on what you are trying to do. Try something like:

f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b|
  b.label { b.radio_button + image_tag(b.object.image) }
end


来源:https://stackoverflow.com/questions/24580256/use-an-image-for-radio-button-label-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!