Rails form submit problem having 2 forms - ruby-on-rails

I have that problem it is always the same form that gets submitted.
The update_limits action gets called on the update order submit button. Which should trigger the action update_order.
Here is my view:
<h2>Movies</h2>
<h3>List movies</h3>
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) %>
<%= link_to 'create new movie', {:action => 'create',:id => params[:id]}, {:class => 'margin-left'} %>
<div class="spacer"> </div>
Number of movies in reel:
<span class="c1">
<% rr = 1..6 %>
<%= select("limits", "reel_limit", rr) %>
</span>
Number of movies in archive:
<span class="c1">
<% rr = 0..12 %>
<%= select("limits", "archive_limit", rr) %>
</span>
<%= submit_tag %>
<div class="spacer"> </div>
<%= form_tag(:controller => 'admin/photographers', :action => 'update_order' ,:id => params[:id]) %>
<ul id='movielist'>
<span class="header">name</span>
<%
n = 0
while n < #items.length
%>
<li itemID='<%=#items[n].id%>' <%= reel_color_class(n, #limits) %>>
<% if #items[n]["image"] %>
<%= image_tag("/photographer/image/#{#items[n].id}/#{#items[n]["image"]}", :size => "36x20" ) %>
<% end %>
<%=#items[n].name.force_encoding("UTF-8") %>
<span class='col2'>
<%= link_to 'edit', {:action => "edit", :id => #items[n].id} %>
<%= link_to("remove", {:action => "remove", :id => #items[n].id },
{:confirm => "Are your sure?"}) %>
</span>
</li>
<%
n = n + 1
end
%>
</ul>
<input type="hidden" name="neworder" id="neworder" value="" />
<input name="commit" type="submit" value="update order" onclick="neworder.value=(junkdrawer.inspectListOrderNew('movielist'))" />
<div class="spacer"> </div>

The form_tag method takes a block, and you are not giving it one. You should be doing something like this:
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) do %>
# form goes here
<% end %>
Or even better, if this is acting on a real object, using a form_for tag:
<%= form_for(#object) do |f| %>
# form_goes here
<% end %>
For more information, please read the Getting Started guide for Rails.

Related

Rails link_to issues

I am trying to make my rails application generate a link without the text and custom content instead. I am currently doing this
<%= link_to '', {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Which generates this code
<a action="show" controller="project" href>
<div class="image-container">
<img alt="House 2" src="/uploads/project_image/image/1/house-2.jpg" title="Ygfaweg">
</div>
<p>
<span class="title">
</span>
<span class="summary">
</span>
</p>
</a>
The link is incorrect and is missing alot of data, any idea why?
Remove '' from link_to method.
Change :controller in link_to method to projects
Remove braces from :id => project.id, :class => "projects"
So, refactor the first line to:
<%= link_to {:controller => :projects, :action => :show}, :id => project.id, :class => "projects" do %>
I didnt come to a pure conclusion with this issue. I ended up doing this which works fine.
<a class="project" href="<%= url_for({:controller => :project, :action => :show, :id => project.id}) %>">
Cheers everyone!
Try to do like this :
<%= link_to ({:controller => :project, :action => :show,:id => project.id}, {:class => "projects"}) do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Hope this will help you.
Just remove blank string from link_to and you are done.
<%= link_to {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>

File upload in Ruby doesn't work when rendering it to another form

I am new to ruby and I would appreciate any kind of help. I have created a file uploader in ruby. When I test it separately, it works perfectly fine! But when I render it into another form, It just doesn't work! Here is my code:
upload/_index.html.erb
<h4>Upload File </h4>
<%= form_tag({:controller => 'upload', :action => 'upload_image'}, :multipart => true, remote: true) do %>
<p><label for="upload_file">Select file..</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload", :class => "btn btn-default btn-lg active", :method => 'post' %>
<% end %>
data_file.rb
class DataFile < ActiveRecord::Base
attr_accessor :upload
belongs_to :event
def self.save_file(upload)
file_name = upload['datafile'].original_filename if (upload['datafile'] !='')
file = upload['datafile'].read
file_type = file_name.split('.').last
new_name_file = Time.now.to_i
name_folder = new_name_file
new_file_name_with_type = "#{new_name_file}." + file_type
directory = 'public/data'
Dir.mkdir(directory + "#{name_folder}");
File.open(directory + "#{name_folder}/" + new_file_name_with_type, "wb") do |f|
f.write(file)
end
end
end
upload_controller.rb
class UploadController < ApplicationController
before_filter :authenticate_user!
before_filter do
redirect_to :root unless current_user && current_user.isAdmin?
end
def index
# #users = User.find(params[:id])
redirect_to new_event_path
end
def upload_image
DataFile.save_file(params[:upload])
#redirect_to new_event_path
end
end
I have rendered upload/index into events/new.html.erb
<div style="margin-left: 360px;"><h1>New event</h1></div>
<br/>
<br/>
<div style="margin-left: 360px;">
<%= form_for(:event, :url => {:controller => 'events', :action => 'new'}, html: { method: :post }) do |f| %>
<% if #event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #event.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :event_Day %><br>
<select name="event_day_id">
<option value="0">-Choose agenda item-</option>
<% for agenda_item in #agenda_items %>
<option value="<%= agenda_item.id %>"><%= agenda_item.name %></option>
<% end %>
</select>
</div>
<%= link_to 'Add new agenda item', new_agenda_item_path %>
<br/>
<br/>
<div class="field">
**<%= render :partial => 'upload/index' %>**
</div>
<br/>
<div class="field">
<div class="field">
<%= f.label :Survey %><br>
<select name="survey_id">
<option value="0">-Choose survey-</option>
<% for survey in #surveys %>
<option value="<%= survey.id %>"><%= survey.title %></option>
<% end %>
</select>
</div>
<%= link_to 'Create new survey', new_survey_path %>
</div>
<div class="actions">
<%= f.submit :class => "btn btn-default btn-lg active", :method => 'post' %>
</div>
<% end %>
</div>
<%= button_to 'Back', events_path, :class => 'btn btn-primary btn-lg active', :method => 'get' %>
Thanks!
Sorry I just noticed that the question was incomplete. I have answered the question though. When rendering the upload file form into another form, it does not work. So I removed it from the form and put it on top and everything is working fine.
Unless there is a way to nest once form inside the other?
Thanks!

Can't get shared filters in home view to work in spree demo

In the spree demo (latest version), I tried to use shared/filters instead of shared/taxonomies in views/spree/home/index.html.erb:
<% content_for :sidebar do %>
<div data-hook="homepage_sidebar_navigation">
<%#= render :partial => 'spree/shared/taxonomies' %>
<%= render :partial => 'spree/shared/filters' %>
</div>
<% end %>
I get the filters (All Taxons), but when I pick some, like Bags and T-Shirts and click on search, the filter doesn't work.
I didn't change anything in shared/filters:
<% filters = #taxon ? #taxon.applicable_filters : [Spree::Core::ProductFilters.all_taxons] %>
<% unless filters.empty? %>
<%= form_tag '', :method => :get, :id => 'sidebar_products_search' do %>
<%= hidden_field_tag 'per_page', params[:per_page] %>
<% filters.each do |filter| %>
<% labels = filter[:labels] || filter[:conds].map {|m,c| [m,m]} %>
<% next if labels.empty? %>
<div class="navigation" data-hook="navigation">
<h6 class="filter-title"> <%= filter[:name] %> </h6>
<ul class="filter_choices">
<% labels.each do |nm,val| %>
<% label = "#{filter[:name]}_#{nm}".gsub(/\s+/,'_') %>
<li class="nowrap">
<input type="checkbox"
id="<%= label %>"
name="search[<%= filter[:scope].to_s %>][]"
value="<%= val %>"
<%= params[:search] && params[:search][filter[:scope]] && params[:search][filter[:scope]].include?(val.to_s) ? "checked" : "" %> />
<label class="nowrap" for="<%= label %>"> <%= nm %> </label>
</li>
<% end %>
</ul>
</div>
<% end %>
<%= submit_tag Spree.t(:search), :name => nil %>
<% end %>
<% end %>
Any ideas why?

Can't find root of 'stack level too deep' error when rendering partial

I've been stuck on this problem for a week. I will mail you a bottle of scotch if you can figure it out. Seriously, its come to bribery.
On taxons#show I'm rendering a products partial, _products.html.erb, which lists out all the products on a table to the show view of the taxons controller. When you click a product, by default the app will redirect the user to products#show, where the _cart_local.html.erb partial is rendered to display 'add to cart' options.
But on taxons#show, when a product is clicked I bring up a lightbox instead so the user doesn't have to leave the page. The lightbox code is inside _products.html.erb, and I'm trying to render _cart_form.html.erb inside of the lightbox. When I do, I get the 'stack level too deep' error and taxons#show won't render.
But the cart renders fine in products#how. I changed #product in the partial to just product. That didn't help. I rendered an empty partial and the page loads, which makes me think the problem is with _cart_local (but why would it render on products#show?).
Then I took out all of the code in between the opening form tag and the ending div/end tags and the page also rendered, which makes me think its in that block, but I can't wittle it down any further. I'm stuck
Here's the code for _cart_local, and if I take out the code between the <!-- Here --> and <!-- AND HERE --> comments, the page renders:
<%= form_for :order, :url => populate_orders_path do |f| %>
<div id="inside-product-cart-form" data-hook="inside_product_cart_form" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<% if product.has_variants? %> <!-- HERE -->
<div id="product-variants" class="columns five alpha">
<h6 class="product-section-title"><%= t(:variants) %></h6>
<ul>
<% has_checked = false
product.variants.active(current_currency).each_with_index do |v,index|
next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
has_checked = true if checked %>
<li>
<%= radio_button_tag "products[#{product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders], 'data-price' => v.price_in(current_currency).display_price %>
<label for="<%= ['products', product.id, v.id].join('_') %>">
<span class="variant-description">
<%= variant_options v %>
</span>
<% if variant_price v %>
<span class="price diff"><%= variant_price v %></span>
<% end %>
</label>
</li>
<% end%>
</ul>
</div>
<% end%>
<% if product.price_in(current_currency) and !product.price.nil? %>
<div data-hook="product_price" class="columns five <% if !product.has_variants? %> alpha <% else %> omega <% end %>">
<div id="product-price">
<h6 class="product-section-title"><%= t(:price) %></h6>
<div><span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span></div>
</div>
<div class="add-to-cart">
<% if product.on_sale? %>
<%= number_field_tag (product.has_variants? ? :quantity : "variants[#{product.master.id}]"),
1, :class => 'title', :in => 1..product.on_hand, :min => 1 %>
<%= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do %>
<%= t(:add_to_cart) %>
<% end %>
<% else %>
<%= content_tag('strong', t(:out_of_stock)) %>
<% end %>
</div>
</div>
<% else %>
<div id="product-price">
<br>
<div><span class="price selling" itemprop="price"><%= t('product_not_available_in_this_currency') %></span></div>
</div>
<% end %> <!-- AND HERE -->
</div>
<% end %>
And here is _products.html.erb, the file that is loading all the products, contains the lightbox, and has the render cart partial code:
<div class="overlay-container">
</div>
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% if products.empty? %>
<%= t(:no_products_found) %>
<% elsif params.key?(:keywords) %>
<h6 class="search-results-title"><%= t(:search_results, :keywords => h(params[:keywords])) %></h6>
<% end %>
<div class="product_grid_container">
<div class="grid_2"><%= image_tag("store/featured/#{#featured}.jpg") %></div>
<% if products.any? %>
<ul id="products" class="inline product-listing" data-hook>
<% products.each do |product| %>
<% if product.on_display? %>
<%# ******LIGHTBOX******* %>
<div id="product_popup_<%= product.id %>" class="product_popup" data-popid="<%= product.id %>">
<div class="related-products">
<ul class="related_products_list" id="related_products_list_<%= product.id %>" data-listid="<%= product.id %>">
<% #related_products.each do |related_product| %>
<li class="related_products_item"><%= link_to large_image(related_product, :itemprop => "image", :data => {:imageid => related_product.id}, :id => "related_" + related_product.id.to_s, :class => "related_products_image dimmed"), url_for(related_product) %></li>
<% end %>
</ul>
</div>
<div class="popup-image">
<%= large_image(product, :itemprop => "image", :class => "product-image-popup") %>
</div><!-- popup-image -->
<div class="popup_right_content">
<h2 class="popup-title"><%= product.name %></h2>
<p class="popup-price">$<%= product.price %></p>
<p><%= product.description %></p>
<p class="popup-color">color:</p>
<div class="popup-images" data-productid="<%= product.id %>">
<% if (product.images + product.variant_images).uniq.size > 1 %>
<ul id="popup-thumbnails-taxon" class="thumbnails inline" data-hook>
<% product.images.each do |i| %>
<li class='tmb-all' id='tmb-<%= i.id %>'>
<%= link_to(image_tag(i.attachment.url(:small)), i.attachment.url(:popup), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
</li>
<% end %>
</ul>
<% end %>
</div><!-- popup-images -->
</div><!-- popup_right_content -->
<%= render 'spree/shared/cart_local', :locals => {:product => product} %>
</div><!-- product_popup -->
<%# ******END LIGHTBOX******* %>
<div class="grid_1">
<li id="product_<%= product.id %>" class="columns product three <%= cycle("alpha", "secondary", "", "omega secondary", :name => "classes") %>" data-hook="products_list_item" itemscope itemtype="http://schema.org/Product">
<div class="main-image" id="single_<%= product.id %>" data-productid="<%= product.id %>">
<%= link_to large_image(product, :itemprop => "image", :class => "product-image", :id => product.id), product_path(product), :remote => true, :html => {:class => "product_popup"} %>
</div><!-- main-image-->
<div class="prod_info_box">
<%= link_to truncate(product.name, :length => 50), product, :class => 'info', :itemprop => "name", :title => product.name %>
<span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span>
<!-- BRINGS THUMBNAILS INTO TAXONS PAGE -- PULLED FROM _THUMBNAILS.HTML.ERB -->
<div class="product-images" data-productid="<%= product.id %>">
<% if (product.images + product.variant_images).uniq.size > 1 %>
<ul id="product-thumbnails-taxon" class="thumbnails inline" data-hook>
<% product.images.each do |i| %>
<li class='tmb-all' id='tmb-<%= i.id %>'>
<%= link_to(image_tag(i.attachment.url(:mini)), i.attachment.url(:normal), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
</li>
<% end %>
</ul>
<% end %>
</div><!-- product-images -->
<!-- END THUMBNAILS INTO TAXONS PAGE -->
<div id="product-description-taxon">
<p><%= product.description %></p>
</div><!-- product-description-taxon -->
</div><!-- prod_info_box -->
</li>
</div>
<% end %>
<% end %>
<% reset_cycle("classes") %>
</ul>
<% end %>
</div><!-- product_grid_container -->
<% if paginated_products.respond_to?(:num_pages) %>
<%= paginate paginated_products %>
<% end %>
Let me know if you need anything else. i appreciate it.
Here's a link to the helpers, maybe the problem is there?
https://github.com/spree/spree/tree/v1.3.2/core/app/helpers/spree
In your _products.html.erb partial change that:
<%= render 'spree/shared/cart_local', :locals => {:product => product} %>
to that:
<%= render partial: 'spree/shared/cart_local', :locals => {:product => product} %>
and problem should be resolved.
Why? Because doing it as you have done it won't pass locals into partial and that's why you've received error, check it by yourself by removing locals. Of course the most interesting part is why you get stack level too deep error here, but I am unable to find answer for that now.
Oh, and debugger is your friend ;)

Rails how to update image? And how to do RESTful routing?

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.
When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID
And my image is not updated.
My form:
<%= simple_form_for #photographer, :url => save_admin_photographers_path(#photographer), :html => {:multipart => true, :method => :post} do |f| %>
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<%= javascript_include_tag "init_my_tiny_mce" %>
<%= f.input :name, :label => 'Name' %>
<%= f.text_area :text, :label => 'Text', :size => '12x12' %>
<%= f.file_field :image, :label => 'Image' %>
<% if #photographer %>
<% if #photographer.image %>
<p class="ok">
<label for="dd">image ok</label>
<%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{#photographer.id}/#{#photographer["image"]}", :size => "39x22") %>
</p>
<p>
<label for="sdd"> </label>
<%= link_to("remove", {:action => "remove_image", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No image uploaded for movie
</p>
<% end %>
<br />
<% end %>
<br />
<%= f.file_field :flv, :label => 'Upload FLV' %>
<br />
<% if #photographer %>
<% if #photographer.flv %>
<p class="ok">
<label for="dd">flv: <%= #photographer["flv"] %></label>
<%= link_to("remove", {:action => "remove_flv", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
No flv uploaded
<br /><br />
</p>
<% end %>
<% end %>
<br />
<%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
<br />
<% if #photographer %>
<% if #photographer.quicktime %>
<p class="ok">
<label for="dd">quicktime: <%= #photographer["quicktime"] %></label>
<%= link_to("remove", {:action => "remove_quicktime", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No quicktime uploaded
<br />
</p>
<% end %>
<% end %>
<%= f.button :submit, :value => 'Create movie' %>
<% end %>
My update controller:
def save
#photographer = Photographer.find(params[:id])
#photographer.update_attributes(params[:photographer])
if !#photographer.save
flash[:notice] = " "
render_action 'edit'
else
flash[:notice] = "update ok"
redirect_to :action => 'edit', :id => #photographer.id
end
end
My routes:
namespace :admin do
resources :photographers do
collection do
post :save
end
end
end
What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.
You should rename your controller action from save to update and remove the custom route:
namespace :admin do
resources :photographers
end
Then use the update route in your form:
:url => admin_photographer_path(#photographer)
You should also change your html method, the update action uses PUT:
:method => :put
Hope this helps.

Resources