I have a form that is going to be used, but i run into this problem when opening the form:
NoMethodError in Measures#new
Showing /home/supervisor/Rubyrails/Werkvergunning/app/views/measures/_form.html.erb where line #22 raised:
undefined method `measurement_type' for #<Measure:0x007f7bb3c5f978>
Did you mean? measurement
measurement?
measurement=
measurement_was
I am absolutely positive that the field exists and other fields from the same table used to work without problem, so it is just thos one field.
What is going on and how can i fix it?
The _form:
<%= form_for(#measure) do |f| %>
<% if #measure.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#measure.errors.count, "error") %> prohibited this measure from being saved:</h2>
<ul>
<% #measure.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :measurement %><br>
<%= f.text_field :measurement %>
</div>
<div class="field">
<%= f.label :type %><br>
<%# f.select :measurement_type, [['Product verklaring','product vrij verklaring'],['Elektrotechnische maatregel','Elektrotechnische maatregelen'],['Maatregel door vergunninghouder','maatregel door vergunninghouder'], ['brandpreventie', 'brandpreventie'], ['persoonnlijke bescherming', 'persoonlijke bescherming']] %>
<%= f.text_field :measurement_type %>
</div>
<div class="field">
<%= f.label :valid_from %><br>
<%= f.date_select :valid_from %>
</div>
<div class="field">
<%= f.label :valid_to %><br>
<%= f.date_select :valid_to %>
</div>
<div class="field">
<%= f.label :enquiry_measure_id %><br>
<%= f.text_field :enquiry_measure_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The table:
create_table "measures", force: :cascade do |t|
t.string "measurement", limit: 255
t.string "measurement_type", limit: 255
t.date "valid_from"
t.date "valid_to"
t.integer "enquiry_measure_id", limit: 4
end
Thanks
Related
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| %>
I have a Post model and Tag model with many to many relationships.
Post Model:
class Post < ActiveRecord::Base
has_and_belongs_to_many :tags
end
Tag model:
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end
I also have a join table for posts_tags :
class JoinPostsAndTags < ActiveRecord::Migration
def change
create_table :posts_tags do |t|
t.integer :tag_id
t.integer :post_id
t.timestamps null: false
end
end
end
Now, I need to provide multiple selection for selecting tags for a post.
Below is the post form.html.erb
<%= 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 :Name %><br>
<%= f.text_field :Name %>
</div>
<div class="field">
<%= f.label :Email %><br>
<%= f.text_field :Email %>
</div>
<div class="field">
<%= f.label :Message %><br>
<%= f.text_area :Message %>
</div>
<% #tags= Tag.all %>
<% if #tags %>
<% #tags.each do |tag| %>
<div>
<%= check_box_tag "post[tag_ids][]", tag.id, #post.tags.include?(tag) %>
<%= tag.name %>
</div>
<% end %>
<% end %>
<br><br>
<%= link_to 'Create Tag', tags_path %>
<br><br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
It is not adding the selected tags to the post. I need to add the selected tags to the post. How can I do it.
But, in rails console if I use post= Post.first tag= Tag.first post.tags<<tag it adds the tag to the post.
I don't have any special code in post controller to handle this. Please help me.
Add {tag_ids:[]} to your params permit arguments in your PostsController, like so:
def post_params
params.require(:post).permit(:name, :email, :message, {tag_ids:[]})
end
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.
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.
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) %>