I have a Rails 3 application with a pretty standard multipart form that includes an image upload. However, it throws an error.
The form starts with:
<%= form_for(#object, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :photo %>
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit 'Submit Object' %>
</div>
<% end %>
Then, I have this in the object.rb model:
def photo=(file_data)
unless file_data.blank
#file_data = file_data
self.extension = file_data.original_filename.split('.').last.downcase
end
end
This throws the following error:
undefined method `blank' for #ActionDispatch::Http::UploadedFile:0x37ecc78
Meanwhile, it also shows the image exists in the Request Parameters error page:
"photo"=>#ActionDispatch::Http::UploadedFile:0x37ecc78 #original_filename="Image.jpg",
Any help would be greatly appreciated!
The blank method doesn't exist. It's blank?, with the question mark.
unless file_data.blank?
...
Documentation
Related
I'm new on rails. I work on a project, and I try to add physical address to my users. I want that the address can be help-complete with Google map for later exploitation. I find the jt-rails-address which look like perfect for my project. But I can't implement it. I need complete address (street, zip code, city & country).
add_address_to_users.rb :
class AddAddressToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :address, :address
end
end
form edit.html.erb :
<div class="col-md-6 col-md-offset-3">
<%= form_for (#user), :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :phone, "Téléphone :" %>
<%= f.phone_field :phone, class: 'form-control' %>
<%= f.label :address, "Addresse :" %>
<div class="jt-address-autocomplete">
<!-- This field is used to search the address on Google Maps -->
<%= f.text_field :address, class: 'jt-address-search' %>
<!-- All fields are hidden because the javascript will set their value automatically -->
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{street}", class: "jt-address-field-#{street}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{zip_code}", class: "jt-address-field-#{zip_code}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{city}", class: "jt-address-field-#{city}" %>
<% end %>
<% for attr in JT::Rails::Address.fields %>
<%= f.hidden_field "address_#{country}", class: "jt-address-field-#{country}" %>
<% end %>
</div>
<%= f.submit "Enregistrer les changements", class: "btn btn-primary" %>
<% end %>
</div>
application.js :
// This function is call when Google Maps is loaded
window.googleMapInitialize = function(){
// Simple usage
$('.jt-address-autocomplete').jt_address();
};
I have already put :address in my user_params in the users controller and has_address :address in the user model
I also have put my Google Api.
My actual error:
undefined local variable or method `street' for #<#Class:0x00007fe91ec7f768:0x00007fe91dfaefc0>
Thanks in advance for your help.
No more error but a bug, maybe because of Google Map, I don't know how
to resolve it, I can't write the address and there is an error message:
problem
Here the code of the inspector:
First, you should add fields like in documentation or scope them using select, now you have code that will create duplicated hidden fields.
You should check from rails console that method address is available on User instance of model.` Maybe you forgot to run migration or didn't restart server after installing this gem as this is js gem and will need restart.
Hope this answers your questions. Add comment to this answer if problem is still there.
I have a Rails 3.2 app with this validate in the costquestion model:
validate :estimatesource
private
def estimatesource
if !(source_contractor? ^ source_other? ^ source_sqft_cost? ^ source_rs_means? ^ source_facilities_management?)
errors.add(:base, "Specify Estimate Source")
end
end
In the form, I have this:
<%= simple_form_for #costquestion, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
<%= f.error_notification %>
At the top of the page it displays "Some errors were found, please take a look:". I don't see the "Some errors were found, please take a look:" error.
How can I get it to display?
Thanks for the help!
SimpleForm highlights fields with errors that are visible. To display errors attached to base, you could use this:
<%= f.error :base %>
Or you can create your own helper to display list of errors at the top
<%- if #costquestion.errors.any? %>
<%- #costquestion.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
<% end %>
I am trying to build a log in system by this tutorial:
http://www.youtube.com/watch?v=h0k6DFIStFY
My form looks like this:
<!DOCTYPE html>
<div id="content">
<%= flash[:alert1] %>
<%= form_for(:sessions, :url => sessions_path , :html => {:id => "login-form"}) do |f| %>
<fieldset>
<p>
<%= label_tag :name ,"Username:" %>
<%= text_field_tag :name, params[:name] , :class => "round full-width-input", :autofocus=>true %>
</p>
<p>
<%= label_tag :password, "Password:" %>
<%= password_field_tag :password, params[:password], :class => "round full-width-input" %>
</p>
<%= submit_tag "Login", :class=> "button round blue image-right ic-right-arrow" %>
</fieldset>
<% if (flash[:status] == FALSE) %>
<br/><div class="information-box round"><%= flash[:alert] %></div>
<% end %>
<% end %>
</div> <!-- end content -->
and my controller looks like this:
class SessionsController < ApplicationController
def login
end
def create
user = User.authenticated?(params[:sessions][:name], params[:sessions][:password])
flash[:alert1] = "dummy"
if user
redirect_to '/login'
else
flash[:status] = FALSE
flash[:alert] = "Invalid username and password"
redirect_to '/login'
end
end
def new
end
end
when trying to submit, i get this error:
undefined method `[]' for nil:NilClass
in the following line:
user = User.authenticated?(params[:session][:name], params[:session][:password])
Did i use incurrectly in the session key ?
Thanks,
Gal!
I think you have some problems in your form: you are using a form_for and then in fields you are using text_field_tag.
I would correct it in something like :
<% form_for sessions .... do |f| %>
<%= f.text_field :name %>
and so forth.
This will generate the params you want in your controller
params[:sessions][:name]
params[:sessions][:password]
I would suggest you to use some gem instead of building an entire system of authentication, which can be quite tricky in terms of security. Have you taken a look at https://github.com/plataformatec/devise?
Hope it helps
It looks like you're using an external authentication gem, perhaps one of these?
https://www.ruby-toolbox.com/categories/rails_authentication
You need to include a require <gem_name> line at the top.
I have an issue retrieving my file upload information. I am just starting to learn Rails.
I am using ruby 2.0.0p0
And Rails 4.0.0.beta1
Here is my form:
<%= form_for(#person, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And in my person.rb model:
def photo=(file_data)
logger.debug("PHOTO")
logger.debug(file_data)
logger.debug("END OUTPUT PHOTO")
unless file_data.blank?
#file_data = file_data
self.extension = file_data.original_filename.split('.').last.downcase
end
end
I can see in my console that nothing happens (no "PHOTO" output), the photo method is never called.
Why is that?
When I looked in the console I also saw this line that got me worried:
Unpermitted parameters: photo
What does that mean?
In your controller, where you're dealing with params, you need to use .permit to list the attributes the form is allowed to post:
#person = Person.new(params.require(:person).permit(:photo))
Alternatively, if you used Rails' scaffolding generator, you might instead have a person_params method where you would need to add the :photo attribute:
def person_params
params.require(:person).permit(:photo, :name, etc...)
end
The -Form_html.erb, the routes.rb and the create method in the controller are as below. But on form submission, it gives nil class error the moment I use params[:mail_setting]
routes.rb
-----------
resources :mail_settings
the _form.html.erb
---------
<%= form_tag '/mail_settings' do %>
<div class="fieldBlock">
<%= label_tag :name %> <%= text_field_tag :name%> </div>
<div class="fieldBlock">
<%= label_tag :id%> <%= text_field_tag :id%> </div>
<div class="actions fieldBlock">
<%= submit_tag "Update Settings ", :class => "btn-large btn-success" %>
</div>
<% end %>
The create method in controller:
def create
#mail_setting = MailSetting.find_by_user_id_and_name(current_user.id, params[:mail_setting][:name])
if ! #mail_setting.blank?
#mail_setting.update_attributes(params[:mail_setting])
else
#mail_setting = MailSetting.new(params[:mail_setting])
#mail_setting.save
render action: "index"
end
end
Try using params[:mailsetting] without the underscore. The model is named MailSetting, so I assume that would be right. The NoMethod error means that there is no generated helper method by the name of mail_setting, which, since you already made the model, is probably because it's spelled wrong.
I would recommend not using underscores/dashes when naming your resources anyways since it just confuses the grammar.