I have created a semi-custom Addon function for products. I created a new relationship table in the backend which connects one product to another product (through a checkbox and an ajax request in the backend).
What I am having trouble with is how to manipulate the populate_orders_path so that I can pass various variant/product id's to the cart at the same time.
Currently in my cart form partial I have this:
<div id="product-addons" class="col-md-6">
<h3 class="product-section-title">Add Ons</h3>
<%= image_tag('short-line-2.png', class: 'hundy') %>
<% addons = Addon.where(product_id: #product.id) %>
<% addons.each_with_index do |a, index| %>
<%= a.inspect %>
<% product_addon = Spree::Product.where(id: a.product_addon_id).first %>
<% addon_variant = Spree::Variant.where(product_id: a.product_addon_id).first %>
<% addon_image = Spree::Asset.where(viewable_id: addon_variant.id).first %>
<div class="col-lg-12">
<%= image_tag('/spree/products/' + addon_image.id.to_s + '/mini/' + product_addon.images[0].attachment_file_name) %>
<%= check_box_tag "products[#{a.id}][]", addon_variant.id, index == 0 %>
<span style="margin-left: 6px;"><%= product_addon.name %></span>
<br/>
</div>
<% end %>
</div>
But I am not sure what to do with
<%= hidden_field_tag "variant_id", #product.master.id %> in order for it to use multiple ID's. Any help would be much appreciated.
You can get above functionality by Spree promotion.
Configure the item added to cart rule and add another item to cart by action in promotion section.
it will work like this if Variant A is added to cart then Variant B will be added to cart automatically.
Related
Ok so Ive looked around at many answers posted on stackoverflow and many other sites because I know that this is a commonly asked question but I just cant seem to get it and would really appreciate it if someone could point me in the right direction. What Im trying to achieve here is really simple, Im trying to render a new form from another model, Post into my homepage, which is another controller that I created. So basically in my homepage it shows a list of all the recent Post that were created by users. Furthermore I wanted to user to be able to directly add a new Post from the homepage. Right now I have it displaying however clicking on the submit button does absolutely nothing.
Here is the form for POST
<div class="panel panel-primary">
<div class="container">
<div class="panel-heading">
<%= image_tag #post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= #post.user.name %>
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.file_field :avatar %>
</div>
</div>
</div>
<div class="panel-footer">
<div class="actions">
<%= f.submit %>
</div>
</div>
<% end %>
</div>
Here is my homepage controller
class HomeController < ApplicationController
def index
if user_signed_in?
#posts = Post.all
#post = current_user.posts.build
else
redirect_to new_user_registration_path
end
end
def create
#post = current_user.posts.build(post_params)
#post.save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :content, :avatar)
end
end
Lastly my homepage index
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-5">
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<%= render 'posts/form' %>
<% #posts.each do |post|%>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<%= image_tag post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= post.user.name %>
</h3>
</div>
<div class="panel-body">
<%= post.content %></br></br>
<% if post.avatar.file? %>
<%= image_tag post.avatar.url(:test), class:"img-thumbnail" %></br>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
Routes
Rails.application.routes.draw do
resources :posts
get 'users/index'
get 'users/show'
root 'home#index'
devise_for :users
So, you're doing like saving post data using another Controller(Home) instead of Post Controller, right?
Your partial form for post uses:
<%= form_for(#post) do |f| %>
// #post generated url (/posts)
// will look for create method in POST Controller
I suggest to put your saving codes in proper controller(posts), so you will prevent code duplication/redundancy.
TIP:
If you are worrying about redirection from Home view or from Posts view,
you can use params[:controller] for checking where the request came from, by just using if statement.
Finally, you can redirect it to proper page after saving.
Right now I've the following piece of code, which retrieves all images attached to Spree products.
<% #product.images_by_variant.each do |variant_id,images| %>
<ul class="gallery gallery-<%= variant_id %>" data-variant="<%= variant_id %>">
<% images.each do |image| %>
<li>
<%= image_tag(image.attachment.url(:single)) %>
</li>
<% end %>
</ul>
<% end %>
However, there are also images displayed that represent all variants. I want to hide those.
How can I only show variant (different colors for example) for products?
Cheers!
I found the solution. In the products_controller_decorator.rb, I added the following piece of code to the show method:
variants = #product.variants
if variants.blank?
#variants = #product
elsif variants.length > 1
#variants = variants.reject {|variant| variant.is_master? }
end
In the view I rendered the following partial:
<% if #variants == #product %>
<%= render partial: 'product_single/product_gallery', locals: {variant: #product} %>
<% else %>
<% #variants.each do |variant| %>
<%= render partial: 'product_single/product_gallery', locals: {variant: variant} %>
<% end %>
<% end %>
I have a mongo rails 4 app, with an embedded photo.
I have no problem in the new form, but in the edit form, if the user has not upload any photo, the form will not display a "add photo button".
Here is the form:
<%= f.fields_for :founder_profile_photos do |founder_photo_f| %>
<%= render partial: 'founder_profile_photo_fields', locals: { f: founder_photo_f } %>
<%= link_to_add_association raw('<i class="fi-plus"> add a founder photo</i>'), f, :founder_profile_photos %>
<% end %>
Here is the partial:
<div class="nested-fields">
<% if f.object.file.to_s.empty? %>
<%= f.file_field :file, label: "Upload a founder photo." %>
<% else %>
<%= image_tag f.object.file, class: 'small-10 medium-10 image-previewer' %>
<% end %>
</div>
Thank you so much for your help. I finally found what was wrong, I needed to build the field photo in the edit action of my controller, other the field does not exist and therefore do not appear in the view.
def edit
if #user.photos.empty?
#user.photos.build
end
end
photo is embedded into the user model (one to many relation) if you have a one to one relation you have to do:
if #user.photo.blank?
#user.photo = Photo.new
end
Thank you very much for all your help, still so much to learn.
When a potential User is editing their Profile, they have an option to show/hide some information from public view. I did that with
<div class="field">
<%= f.text_field :contact %>
<%= f.select(:contact_type_id, ContactType.all.map {|l| [l.name, l.id]}) %>
<%= f.check_box :visible %>
<%= f.label :visible, "Vidljivo" %>
<%= f.link_to_remove "Izbriši kontakt" %>
</div>
in a partial that's called in the view like this
<%= f.fields_for :contacts do |l| %>
<%= render 'contacts_form', f: l %>
<% end %>
What I now want is to display on a public profile page only that information that the User marked as visible, and I tried it through a different partial
<div class="field">
<% if :visible == true %>
<%= f.text_field :contact %>
<% end %>
</div>
called from
<%= f.fields_for :contacts do |l| %>
<%= render 'contacts_show', f: l %>
<% end %>
but it doesn't work. I tried tons of variations, but it all came down to guessing the right syntax. How do I display only those entries which visibility is marked as "true"?
Why are you displaying the public profile as a form? If you only want to show the information you can iterate over the contact information of the user where the contact type is visible.
I solved it by using a different approach. In my user controller I defined an instance variable #visible and passed it the array consisting of all database entries that have visible parameter set to true. Then I used #visible.each in the view to print the desired content.
I have a search form on an index page for my Properties model which is using both Ransack and Geocoder to search the Properties model fields as well as narrow the results down based on distance from an address input by the user in the search form.
The address and distance are captured in the form with :nearaddress and :distance, respectively, which I send in params[:near]. I am checking for the presence of them in the controller index action following this answer. The result is “undefined method `merge' for nil:NilClass” when navigating to /properties, so the view will not render. How do I enable these non-model form parameters to be passed to the controller properly? I think this might be a strong parameters issue but I'm stuck on how to permit these attributes that aren't in the Properties model. The error highlights the "f.text_field :nearaddress" line.
index.html.erb:
...form fields that work when excluding the two that follow...
<div class ="field">
<%= f.label :nearaddress, "Address" %>
<%= f.text_field :nearaddress, params[:near] %>
</div>
<div class ="field">
<%= label_tag :distance %>
<%= text_field_tag :distance, params[:near] %> miles
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
properties_controller.rb:
def index
if params[:near].present? && (params[:near].to_i >0)
#search = Property.near(params[:near]).search(params[:q])
else
#search = Property.search(params[:q])
end
#properties = #search.result(:distinct => true).paginate(:page => params[:page])
...
end
I was able to resolve this problem by removing the "f." and adding "_tag" to the :nearaddress field as well as specifying the params in the controller index:
<%= label_tag :nearaddress, "Near Address" %>
<%= text_field_tag :nearaddress, params[:near] %>
<%= label_tag :distance, "Distance From Address (mi)" %>
<%= text_field_tag :distance, params[:near] %>
<div class="actions"><%= f.submit "Search" %>
<% end %>
if params[:distance].present? && (params[:distance].to_i >0) && params[:nearaddress].present?
#search = Property.near(params[:nearaddress],params[:distance]).search(params[:q])
else
#search = Property.search(params[:q])
end