I'm using the file_field to upload a photo, I've installed gem 'carrierwave' and in the edit view from a user i have this:
<h1>Actualizar</h1>
<div>
<%= form_for current_user, :html => { :multipart => true } do |x| %>
<label>Introduzca su nombre:</label>
<%= x.text_field :name %>
<br>
<!--<label>Introduzca su contraseƱa:</label>
<%= x.password_field :password %>
<br>-->
<label>Introduzca su username:</label>
<%= x.text_field :username %>
<br>
<label>Introduzca su email:</label>
<%= x.text_field :email %>
<br>
<div>
<label>Selecione imagen:</label>
<%= x.file_field :file %>
</div>
<br>
<%= x.submit "Aceptar"%>
<%end%>
</div>
When i open the browser the file_field doesn't appear.
Related
I am using rails form to update record in database. I have a user form. User can enter value in that. I want to display original value present in that record. User can change that according to requirement or let it be as it is. How to do that in embeded ruby or html ? ...
<%= form_with(model: #user1,url: create2_path ,local: true,method: "post" )do |form| %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :password %><br>
<%= form.text_area :password %>
</p>
<p>
<%= form.label :email %><br>
<%= form.text_area :email %>
</p>
<p>
<%= form.label :accounttype %><br>
<%= form.text_area :accounttype %>
</p>
<p>
<%= form.label :phonenumber %><br>
<%= form.text_area :phonenumber %>
</p>
<p>
<%= form.label :status %><br>
<%= form.text_area :status %>
</p>
<p>
<%= form.submit('update') %>
</p>
<% end %>
You can assign the user in the controller (#user) and either use something like simple_form to build the form, or just set input values based on #user, like <input value="<%= #user.name %>">
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 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 %>
<% title "Log in" %>
<p>Don't have an account? <%= link_to "Sign up!", signup_path %></p>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :login, "Username or Email Address" %><br />
<%= text_field_tag :login, params[:login] %>
</p>
<p>
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</p>
<p><%= submit_tag "Log in" %></p>
<% end %>
How can I customise these login fields with CSS and placeholder text? they're not like normal html fields. Thanks
You can take a look at the documentation of FormTagHelper. Changing you form to following would apply the classes to you labels and fields, then you can style them the way you want.
<% title "Log in" %>
<p>Don't have an account? <%= link_to "Sign up!", signup_path %></p>
<%= form_tag sessions_path do %>
<p>
<%= label_tag :login, "Username or Email Address", :class => "login-label" %><br />
<%= text_field_tag :login, params[:login], :class => "login-field" %>
</p>
<p>
<!-- second parameter is the content to be written inside label parameter, you can also pass nil which will humanize the first parameter -->
<%= label_tag :password, "Password", :class => "password-label" %><br />
<!-- second parameter is the value to be populated, you can pass nil if you don't want any -->
<%= password_field_tag :password, nil, :class => "password-field" %>
</p>
<p><%= submit_tag "Log in", :class => "login-button" %></p>
<% end %>
Inline styling is not a good idea IMHO.
They do compile into plain-old HTML fields. For instance
<%= label_tag :password %>
compiles into
<label for="password">Password</label>
You can pass additional options to add additional parameters to the tag. Example:
<%= label_tag :password, "Password", :class => "some_class", :style => "background: green;" %>
compiles to
<label class="some_class" for="password" style="background: green;">Password</label>
The same is true for password_field_tag, text_field_tag, and pretty much any other tag helper.