Pretty new to Ruby, but I'm working on a website for editing information on bus routes for a larger system. The DB has Route separate from it's list of stops, Trips. I'm trying to make the list of stops for each Route easily editable with a list of collection_select forms, where each presents a list of all of the stops to choose from. That was easy with the code below in
routes/_form.html.erb
<!-- handles the list of stops -->
<div class="stops_list" id="stops_list">
<%= f.label "Stops", :class => 'control-label'%>
<% params.merge!(:trips => {}) %>
<% puts params.inspect %>
<% trips = Trip.where('route_id = ?', #route.id).order('trips.order ASC') %>
<% #trips.each do |trip| %>
<%divId = 'stop'+trip[0].to_s %>
<div class="controls" id= <%= divId %>>
<%= f.label trip[0].to_s + '.', :class => 'control-label'%>
<%= collection_select params[:trips], trip[0].to_s, Stop.order('name ASC').all, :id, :name, {:selected => #trips[trip[0]]} %>
</div>
<% end %>
And then I update the proper tables in the DB from the routes_controller based on the data in params. I'd also like to be able to add and remove stops. I tried adding a 'remove stop' button, as below,
<%= button_to_function 'Remove stop',
'if(confirm("Really?")) {
$("#stops_list div").last().remove();
$.get("/routes/removeLastStop/'+(#route.id.to_s)+'");
}'%>
and it removes the proper collection_select form, but I then have to remove the stop from params. I tried doing an ajax call, as you can see, but then I have a new instance of the routes_controller, and the params I want to change is inaccessible. I'm not sure if I just set up the stops list wrong in the first place, or if there's a quick fix, but can anyone more experienced point me in the right direction?
EDIT:
Here's the entire form; it's pretty simple until you reach the area I've added
<%= form_for #route, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :longname, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :longname, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :color, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :color, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :shape, 'Shape (KML file)', :class => 'control-label' %>
<div class="controls">
<%= f.file_field :shape, :class => 'file_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :enabled, :class => 'control-label' %>
<div class="controls">
<%= f.check_box :enabled, :class => 'checkbox' %>
</div>
</div>
<!-- handles the list of stops -->
<div class="stops_list" id="stops_list">
<%= f.label "Stops", :class => 'control-label'%>
<% params.merge!(:trips => {}) %>
<% puts params.inspect %>
<% trips = Trip.where('route_id = ?', #route.id).order('trips.order ASC') %>
<% #trips.each do |trip| %>
<%divId = 'stop'+trip[0].to_s %>
<div class="controls" id= <%= divId %>>
<%= f.label trip[0].to_s + '.', :class => 'control-label'%>
<%= collection_select params[:trips], trip[0].to_s, Stop.order('name ASC').all, :id, :name, {:selected => #trips[trip[0]]} %>
</div>
<% end %>
<%= button_to_function 'Remove stop',
'if(confirm("Really?")) {
$("#stops_list div").last().remove();
$.get("/routes/removeLastStop/'+(#route.id.to_s)+'");
}'%>
</div>
<!-- adds submit button -->
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
routes_path, :class => 'btn' %>
</div>
<% end %>
Firstly you can add to Route class:
class Route < ActiveRecord::Base
has_many :trips
end
Firstly i'd like to advise you using decorators, inserting Models in Views is not the best practice.
Secondly you can use gem 'draper' and in RouteDecorator class you can add functions for creating something you want to use or display.
Thirdly you'd better use:
<div class="controls" id="stop<%= trip[0].to_s %>">
Forthly it would be better to use paths in REST-style:
/routes/2/stop/remove
/routes/2/stop/add
Fifthly can you give code of all form?
Probably we need to refactor it to look better and be easier for understanding.
Related
the checkbox helper isn't updating or inserting boolean values in the database.
A label has many attributes with values false as default and should become true when the checkbox is checked.but only the email attribute is being updated.
labelscontroller
def new
#label = current_user.labels.build
end
def create
#label = current_user.labels.build(label_params)
if #label.save
redirect_to #label
else
render 'new'
end
end
def labels_params
params.require(:label).permit(:name,:email,:avatar,:nick,:intro)
end
label/_form.html.erb
<%= form_for #label, :html => { :class => "form-horizontal label" ,multipart: true} do |f| %>
<div class="form-group", style="color: black;">
<%= f.label :name, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
<%= f.text_field :name, :class => 'form-control' %>
</div>
<%=f.error_span(:name) %>
</div> <div class="form-group", style="color: black;">
<%= f.label :email, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
<%= f.check_box :email %>
</div>
<%=f.error_span(:email) %>
</div>
<div class="form-group", style="color: black;">
<%= f.label :intro, :class => 'control-label col-lg-2' %>
<div class="col-lg-10">
<%= f.check_box :intro, checked: false %>
</div>
<%=f.error_span(:intro) %>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
labels_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
params
developement.rb server logs
database values after the create commit
rails console output of label when i ckecked both email and intro
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 creating a job portal application in Rails 4.1 using Ruby 1.9
The application initially was running fine, but unexpectedly an error popped up. The jobs subdirectory in the views directory contains two files new.html.erb and _form.html.erb(this is a partial file). While rendering the new file, the partial file is also rendered from the new file.
But, the application is showing the following error in Rubymine
ERROR DETAILS:
"NoMethodError in Jobs#new"
Showing E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/_form.html.erb where line #23 raised:
undefined method `error_span' at line 23
20 <div class="controls">
21 <%= f.text_field :title, :class => 'form-control' %>
22 </div>
23 <%= error_span(#job[:title]) %>
24 </div>
25 <div class="control-group">
26 <%= f.label :description, :class => 'control-label' %>
Trace of template inclusion: app/views/jobs/new.html.erb
Rails.root: E:/RailsProject/Old/FinalSubm/jobportal
Application Trace | Framework Trace | Full Trace
app/views/jobs/_form.html.erb:23:in block in _app_views_jobs__form_html_erb__604570268_42022236'
app/views/jobs/_form.html.erb:1:in_app_views_jobs__form_html_erb__604570268_42022236' app/views/jobs/new.html.erb:5:in `_app_views_jobs_new_html_erb___144564004_42034104'
The following is the code written for E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/_form.html.erb
<%= form_for #job, :html => { :class => "form-horizontal job" } do |f| %>
<% if #job.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'form-control' %>
</div>
<%= error_span(#job[:title]) %>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'form-control' %>
</div>
<%= error_span(#job[:description]) %>
</div>
<div class="control-group">
<%= f.label :tag1, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag1, options_for_select(%w[Internship Full-Time Part-Time Co-Op Contractor]) %>
</div>
<%= error_span(#job[:tag1]) %>
</div>
<div class="control-group">
<%= f.label :tag2, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag2, options_for_select(%w[Work-From-Home Office]) %>
</div>
<%= error_span(#job[:tag2]) %>
</div>
<div class="control-group">
<%= f.label :tag3, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag3, options_for_select(%w[US-Citizen Non-US-Citizen]) %>
</div>
<%= error_span(#job[:tag3]) %>
</div>
<div class="control-group">
<%= f.label :category, :class => 'control-label' %>
<div class="controls">
<%= f.select :category_name, Category.all.collect{ |c| [c.name, c.id]} %>
</div>
<%= error_span(#job[:category_name]) %>
</div>
<div class="control-group">
<%= f.label :deadline, :class => 'control-label' %>
<div class="controls">
<%= f.date_select :deadline, :class => 'form-control' %>
</div>
<%= error_span(#job[:deadline]) %>
</div>
<!-- <div class="control-group">
<%= f.label :employer_id, :class => 'control-label' %>
<div class="controls">
<%= f.number_field :employer_id, :class => 'form-control' %>
</div>
<%= error_span(#job[:employer_id]) %>
</div>
<%= f.hidden_field :employer_id %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
jobs_path, :class => 'btn btn-default' %>
<% end %>
The following is the code written for E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/new.html.erb
<%- model_class = Job -%>
<div class="page-header">
<h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>
The program was running fine a couple of days back, no changes have been made to the code since then, apart from restarting the rails server. I am trying to figure out where this error popped from. To solve this error should I define the method error_span in Jobs#new controller ?. What should be written in it ? OR Should I remove the error_span statement from the views, but this would imply that many changes would have to be made in the code, since I have 8 to 10 more files where the partial file mentioning the html statement needs to be changed.
If you're using twitter-bootstrap-rails (which is where I'm guessing the error_span method comes from).
Try using the latest code by putting in your Gemfile:
gem 'twitter-bootstrap-rails', :git => 'https://github.com/seyhunak/twitter-bootstrap-rails.git', :branch => 'master'
Because it looks like they fixed this method a few weeks ago.
more info:https://github.com/seyhunak/twitter-bootstrap-rails/pull/795
twitter-bootstrap-railshas a bug in lib/generators/bootstrap/themed/templates/
<%%= error_span(#<%= resource_name %>[:<%= column.name %>]) %>
change to
<%%= f.error_span(:<%= column.name %>) %>
.erb && slim templete has the same bug.
here is a fixed version
Based on #NoelProf's answer, I changed my Gemfile:
gem 'twitter-bootstrap-rails', '>= 3.2.0', '< 3.2.2'
This will not install the version 3.2.2.
I restarted the server and the error_span problem has been gone.
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.
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 %>