I'm making a Rails app, and want to add an upload feature with allows users to upload multiple entries at once through an Excel spreadsheet as opposed to entering one entry at a time.
Ideally, I was hoping to add a separate Upload/Submit portion to the bottom of the new.html.erb file (the bold portion being the Upload HTML.erb):
...
<div class="actions">
<%= f.submit %>
</div>
<% end %>
**<div class="field">
Or Upload Multiple Entries <br />
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'multi_doc' %>
<% end %>
</div>**
Here is my routes.rb file:
Dataway::Application.routes.draw do
devise_for :users
resources :revenue_models do
get 'upload', :on => :collection
end
root :to => "home#index"
end
And my revenue_models_controller (haven't developed the action at all yet, now just a redirect):
...
def upload
redirect_to revenue_models_path
end
I have followed the rails guides for Uploading files as well as for Routing files and I keep getting an error when I attempt to open the /new view I have modified:
Routing Error
No route matches {:action=>"upload", :controller=>"revenue_models"}
Try running rake routes
When I run rake route, I get an entry for the upload action:
upload_revenue_models GET /revenue_models/upload(.:format) revenue_models#upload
In the end, what I would like to do is upload an excel file with multiple entries, parse it, and conditionally add each data row to my database, which I was under the impression I could specify in the upload action. Please help!
Please use form_for instead form_tag.
Second What is multi_doc is a field/attribute of revenue_model?. I guess is a field/attribute of revenue_model.
Third, Where is your instance variable revenue_model in the form?
Fourth, You have to create first a instance variable of your Model, on your action/controller where you are showing the form, for example #revenue_model = RevenueModel.new.
After try this code:
<%= form_for(#revenue_model, :method => :get, :html => {:multipart => true}, :url => { :controller => "revenue_models", :action => "upload"})) do |f| %>
<%= f.file_field :multi_doc %>
<%= f.submit :id => "any_id" %>
<% end %>
The question is very confusing, trying to clarify your question specifying the name of your actions and the names of the files in your views.
Regards!
Related
I'm having a hard time with the Paperclip gem. I'm trying to upload a picture as an avatar for my user. Everytime I upload, it will get the file and store it in the User DB table. I'm trying to get my simple form to go through action='/users/<%=session[:user_id]%>', which then will go through user#update (which then will update my users picture), but it just gives me an error:
`undefined local variable or method `update_users_path' for '
form_for #user, :url => update_users_path, :method => patch, :html => {:multipart => true} do |f| %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<div class="form-action">
<%= f.submit :submit %>
</div>
<% end %>
I tried to do it without Simple Form, but I need multipart to be true in order to pass back the file.
The path to update a User is not update_users_path, it is user_path(#user) with :method =>:patch or :method => :put. RESTful routing connects PUT /users/:id to the #update action. Take a look at the Rails Routing guide for more details.
You should be able to build the form properly with just:
form_for #user, :method => :patch, :html => {:multipart => true}
The URL will be inferred from the method and #user, or if you do want to include it, use :url => user_path(#user). In fact, because the #user already exists, Rails will infer :method for you too.
Since you are using form_for (and presumably a recent version of Rails), you also don't need to explicitly add multipart. As the Form Helpers guide indicates, Rails will figure that out from the presence of f.file_field and include it for you.
The error is that update_users_path is not a valid route.
--
The route will be user_path(#user) with method: :patch (if you're using resources :users in the routes)
If you have #user populated properly, you shouldn't have issue by just passing it directly to the form_for as follows:
#app/views/users/edit.html.erb
<%= form_for #user, multipart: true do |f| %>
I am new to rails. Today I encountered a problem that I have no clue how to fix it.
Basically I am trying to put a input area and a submit button on one webpage, and the input values is stored in the params[:name], passing to the export_issues method defined in issues controller.
this is what the view file looks like
<%= form_tag(:controller => 'issues', :action => 'export_issues') do%>
<p>
<%= label_tag :name, "name:" %>
<%= text_field_tag :name, params[:name]%>
</p>
<%= submit_tag "Submit" %>
when i click the 'Submit' I got "Routing error". But if I just press F5 refresh the error pageor type 127.0.0.1/issues/export_issues it will work just as I wanted
and this is the code related to issues controller in routes.rb
resources :issues, :only => [:index, :destroy] do
member do
post 'create_comment'
get 'mark_readed'
end
collection do
get 'export_issues'
delete 'destroy_comment'
end
end
basically what the export_issues does is to read the database and export data to a CSV file.
It worked fine without the form_tag codes taking part in.
So what is the problem ?
The reason is simple. By default form_tag creates form element with method=post. Like:
form_tag('/myposts')
will create form tag as follows:
<form action="/myposts" method="post">
However, our routing says, it accepts only get. So, the form opening tag should be:
<%= form_tag(:controller => 'issues', :action => 'export_issues', :method => :get) do%>
For more information, please consult the apidocs.
I'm trying to create a form where a user can type in their email and it will save it to the db. I want this form in the footer of every page.
I've generated NewsletterSignup via rails scaffolding.
Now i have this code in my /app/views/refinery/_footer.html.erb:
<%= form_for(#newsletter_signup) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and when i try and load the page i get this error:
NoMethodError in Refinery/pages#home
Showing /Users/tomcaflisch/Sites/PersonalTrainingKT/app/views/refinery/_form.html.erb where line #1 raised:
undefined method `newsletter_signups_path' for #<#<Class:0x007fb84c769ba0>:0x007fb84c08d110>
Why is the method undefined? If i run rake routes i can see it exists:
newsletter_signups GET /newsletter_signups(.:format) newsletter_signups#index
POST /newsletter_signups(.:format) newsletter_signups#create
new_newsletter_signup GET /newsletter_signups/new(.:format) newsletter_signups#new
edit_newsletter_signup GET /newsletter_signups/:id/edit(.:format) newsletter_signups#edit
newsletter_signup GET /newsletter_signups/:id(.:format) newsletter_signups#show
PUT /newsletter_signups/:id(.:format) newsletter_signups#update
DELETE /newsletter_signups/:id(.:format) newsletter_signups#destroy
routes.rb
PersonalTrainingKT::Application.routes.draw do
resources :newsletter_signups
# This line mounts Refinery's routes at the root of your application.
# This means, any requests to the root URL of your application will go to Refinery::PagesController#home.
# If you would like to change where this extension is mounted, simply change the :at option to something different.
#
# We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"
mount Refinery::Core::Engine, :at => '/'
end
I'm also creating a new object in the application_controller since this newsletter signup will be available on every page:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :instantiate_newsletter_signup
def instantiate_newsletter_signup
#newsletter_signup = NewsletterSignup.new
end
end
It looks like since refinerycms mounts the routes (not sure if that's correct terminology), it cannot see normal routes.
From this link https://groups.google.com/forum/#!topic/refinery-cms/5k7Co4D1bVI I figured out that i needed to change
<%= form_for(#newsletter_signup, :url => newsletter_signups_path, :method => :post) do |f| %>
to
<%= form_for(#newsletter_signup, :url => main_app.newsletter_signups_path, :method => :post) do |f| %>
I'm uploading a plain text file and I'd like to use the text within as the parameters for a new object. Here's my attempt so far, in views/scans/new.html.erb:
<h1>New scan</h1>
<%= form_for :file_upload, :html => {:multipart => true} do |f| %>
<p><%= f.file_field :raw %></p>
<p><%= f.submit "Upload" %></p>
<% end %>
raw is a text attribute of the scan model. This yields the error:
Routing Error
No route matches [POST] "/scans/new"
Try running rake routes for more information on available routes.
I can tell from Google that I need to File.read() but I don't know where I would do it.
in model
def raw= (file)
File.open(file,"r") do |f|
self.your_atrribute = f.readlines.join("")
end
end
I have a form field in a view that is separate from my initial form. I want the person using the application to be able to edit a single field without having to pull up the entire form. My code is as follows
<%= form_for :user, :url => {:controller => 'users', :action => 'update' } do |f| %>
<%= f.text_field :barcode %>
<%= submit_tag 'Register' %>
<% end %>
When trying to submit the changes to the specified form field I receive an error on the create method I believe. It redirects me to the user controller with the proper id but gives me the following error.
Unknown action
The action '1' could not be found for UsersController
I have tried changing the method from update to create, but then it brings up the blank form, I just want to be able to edit the specified field without having to re-create the form and get the error. Any ideas?
You are not passing the user object to the form.
Try also using the path helper generated by the routes:
<%= form_for #user, :url => user_path(#user) do |f| %>
<%= form_for(#user), :url => url_for(:controller => 'users', :action => 'update') do |f| %>
<%= f.text_field :barcode %>
<%= f.submit, 'Register' %>
<% end %>
It should work now...