ActionController::UrlGenerationError in Products#edit - html

I'm new to rails and I'm having trouble in a sales site, I have a User and produito the protuto User belongs to, everything works right exeto edit the action of the product.
ActionController::UrlGenerationError in Products#edit
No route matches {:action=>"show", :controller=>"products", :format=>nil, :id=>nil, :user_id=>#} missing required keys: [:id]
my_edit
<h1>Editing product</h1>
<%= render 'form' %>
<%= link_to 'Show', #product %> |
<%= link_to 'Back', products_path %>
my _form
<%= form_for [:user,#product], :html => {multipart: true} do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :local %><br>
<%= f.text_field :local %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all.map {|c| [c.name, c.id]} %>
</div>
<div class="field">
<%= f.label :contacts %><br>
<%= f.text_field :contacts %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
my rake routes
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
category_category GET /categories/:id/category(.:format) categories#category
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
profile_user GET /users/:id/profile(.:format) users#profile
user_products GET /users/:user_id/products(.:format) products#index
POST /users/:user_id/products(.:format) products#create
new_user_product GET /users/:user_id/products/new(.:format) products#new
edit_user_product GET /users/:user_id/products/:id/edit(.:format) products#edit
user_product GET /users/:user_id/products/:id(.:format) products#show
PATCH /users/:user_id/products/:id(.:format) products#update
PUT /users/:user_id/products/:id(.:format) products#update
DELETE /users/:user_id/products/:id(.:format) products#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
root GET / home#index

From your comments I found that you are missing to pass ID for the product.
You need to pass #user & #product objects like this:
<%= link_to 'Edit', edit_user_product_path(#user, #product) %>

Related

Rails - Submitting multiple forms with one submit button

In my application I have a page for editing all users however I would like it so that I only have to click one submit button to update the users' details but at the moment I can only edit one and click the 'Update User' button to change that one user's details.
_form.html.erb
<%= form_tag edit_user_path do |form| %>
<% #users.each do |user| %>
<%= form_for user do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :sunday %><br>
<%= f.text_field :sunday %>
</div>
<div class="field">
<%= f.label :monday %><br>
<%= f.text_field :monday %>
</div>
<div class="field">
<%= f.label :tuesday %><br>
<%= f.text_field :tuesday %>
</div>
<div class="field">
<%= f.label :wednesday %><br>
<%= f.text_field :wednesday %>
</div>
<div class="field">
<%= f.label :thursday %><br>
<%= f.text_field :thursday %>
</div>
<div class="field">
<%= f.label :friday %><br>
<%= f.text_field :friday %>
</div>
<div class="field">
<%= f.label :saturday %><br>
<%= f.text_field :saturday %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
As you can see that submit_tag at the bottom of the code does absolutely nothing however I would like it to update all the users' details. Any help would be greatly appreciated!
Against:
<%= form_for user do |f| %>
use:
<%= fields_for "users[]", user do |f| %>
After that you will get in the controller parameters values for each user_id:
"users"=>{"user_id1"=>{"attr1"=>"value1"}, "user_id2"=>{"attr1"=>"value1"}
Also to make possible updating a collection of object a way to do it is to add an action to the UsersController like this:
def update_collection
# Update users here
end
and update routing in config/routes.rb:
resources :users do
collection do
match 'update_collection', via: [:put, :patch]
end
end
and use proper url in the main form:
<%= form_tag update_collection_users_path, method: :put do |form| %>

Audio has contents that are not what they are reported to be - Paperclip

A beginner in Ruby here!
I am trying to create a Soundcloud clone on ruby.
When I try to upload an audio file i get the error:
1 error prohibited this song from being saved:
Audio has contents that are not what they are reported to be
controller: song.rb
class Song < ActiveRecord::Base
belongs_to :user
has_attached_file :audio,
:url => "/assets/:class/:id/:attachment/:basename.:extension",
:path => ":rails_root/public/assets/:class/:id/:attachment/:basename.:extension"
validates_attachment :audio,
:content_type => { :content_type => ["audio/mpeg", "audio/mp3"] },
:file_name => { :matches => [/mp3\Z/] }
_form.html.erb
<%= form_for(#song) do |f| %>
<% if #song.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#song.errors.count, "error") %> prohibited this song from being saved:</h2>
<ul>
<% #song.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :audio %><br>
<%= f.file_field :audio%>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Audio:</strong>
<%= #song.audio.url %>
</p>
<p>
<strong>Title:</strong>
<%= #song.title %>
</p>
<%= link_to 'Edit', edit_song_path(#song) %> |
<%= link_to 'Back', songs_path %>
In my experience with this problem, image data in the mp3's metadata (i.e. - an album cover) was the critical factor. The image data triggers the content type spoofing mechanism and the record will fail validation. To remove the image metadata, I opened the mp3 with Audacity, and exported it to a new file, which will not include image metadata because Audacity doesn't seem to include this automatically.

gem 'country_select', github: 'stefanpenner/country_select' is not working with rails 4

I am using gem 'country_select', github: 'stefanpenner/country_select' in my gem file and in my form i have defined it like this:
<%= form_for(#account_detail) do |f| %>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :zip %><br>
<%= f.number_field :zip %>
</div>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.country_select("account_detail", "country") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
on submit its giving error ActionView::Template::Error (wrong number of arguments (4 for 0)):
Which gem is best to show all countries?
I would use:
<%= f.country_select :country %>
If you like to prioritize some countries in the select pass them in in an array:
<%= f.country_select :country, {priority_countries: %w(<COUNTRY CODE i.e. US>), prompt: 'Select Country'} %>
You can add class: 'your-class' and id or whatever just as with any other field if you like. Hope it helps.
This should do!
<%= f.country_select :country, { priority_countries: ["GB", "US"], selected: "GB" } %>
I have solved this issue by adding this method in my model:
def country_name
country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
and in view change given line :
<%= f.country_select("account_detail", "country") %>
to this:
<%= f.country_select :country, format: :with_alpha2 %>
Hope this will help to someone else who will face this problem.

Rails 4: devise sign up form styling issues

I used to roll my own authentication following Hartl's tutorial, but now that I need more advanced features such as password retrieval or email confirmation, I moved on to the Devise gem. However, I'm having some hard time navigating around what is already provided by devise. I'm doing some research in order to better understand it.
Meanwhile, I'm having problems with styling the sign in and sign up forms. For the sign up form, it looks fine at first, but the spacing becomes weird once I submit an invalid form.
For the sign in form, I cannot seem to figure out how to put the checkbox and the label on the same line. I tried all different kinds of divs and inline block. Please help me out. Thanks!
/devise/registrations/new.html.erb
<div class="center">
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= field_set_tag "Name" do %>
<%= f.text_field :first_name, placeholder: "First Name*", autofocus: true %><br>
<%= f.text_field :middle_name, placeholder: "Middle Name" %><br>
<%= f.text_field :last_name, placeholder: "Last Name*" %><br>
<% end %>
<%= field_set_tag "Account" do %>
<%= f.email_field :email, placeholder: "Email*" %><br>
<%= f.password_field :password, placeholder: "Password*" %><br>
<%= f.password_field :password_confirmation, placeholder: "Password Confirmation*" %>
<% end %>
<div><%= f.submit "Sign up", class: "btn btn-large btn-primary" %></div>
<% end %>
Already a user? <%= render "devise/shared/links" %>
</div>
/devise/sessions/new.html.erb
<div class="center">
<h2>Sign in</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.email_field :email, placeholder: "Email", autofocus: true %><br>
<%= f.password_field :password, placeholder: "Password" %><br>
<div class="row">
<% if devise_mapping.rememberable? %>
<div>
<div class="remember-me">
<div><%= f.label :remember_me %></div>
<div><%= f.check_box :remember_me %></div>
</div>
</div>
<% end -%>
<%= f.submit "Sign in", class: "btn btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
Check this stackoverflow link you will find the solution for the sign in page issue.
For the sign up page you are getting the spacing issue when input are not correct because your form is not showing the error messeges properly check out this image you will understand.

HTML <form> tag causing Rails form to submit a GET instead of POST request

I have a form that works fine until I add the tags for styling (I am using twitter bootstrap). The app is Rails 3.1 and I am on a mac. Here is a form that works fine with no issues:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
Then, when I add the tag to the same exact form like this:
<form class="form-stacked">
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
</form>
This form appears to issue a GET request instead of a POST request. Instead of redirecting me upon save like it does without the tag, I see this in the URL:
http://localhost:3000/signup?utf8=%E2%9C%93&authenticity_token=Pt1vOp7lykCPFdj5BJeZ6xwJM2vy0JomMGSKoB%2FyYpU%3D&user%5Bemail%5D=test%40get.com&user%5Bpassword%5D=kevin&user%5Bpassword_confirmation%5D=kevin&commit=Create+User
I have tried specifying post in both the rails and HTML. Specifying it in rails using :method => post yields the same results (the GET request and URL info above). Using in the HTML looks like a POST request but the User object isn't saving (I belive this is the case because I am not getting redirected to the root_url, which is set in the controller and works fine when not using tag). Any ideas?
you want this:
<%= form_for #user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
stuff goes in here as before
<% end %>