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| %>
Related
In a rails 4 application, I have a book resource, that is a Book model with its controller, views and route. It's what gets created by:
rails g scaffold book title
Now I want to have another set of views (and another controller) that allows to manage the same model, maybe dedicated to a different user.
I want both the creating function and the editing function to be available on this different route and view, .
Let's call it book2.
The views in the /book2 url should operate on the Book2sController.
form_for support
But the form_for guesses the submit route (and puts it in the action attribute) from the model class, that, being it always Book, lets rails guess that the submit url is /books/1 for edit or /books/ for new and not /book2s/1 for edit and /book2s/ for new as it should be.
So i found this solution, but i find it to be a bit cumbersome.
Is there anything better out there?
<%= form_for #book, :url => #book.new_record? ? url_for(book2s_path) : url_for(book2_path(#book)) do |f| %>
<%= f.text_field :title %>
<% end %>
You could set the url in your controller.
def new
# ...
#form_url = book2s_path
# ...
end
def edit
# ...
#form_url = book2_path(#book)
# ...
end
Then your view becomes:
<%= form_for #book, :url => #form_url do |f| %>
<%= f.text_field :title %>
<% end %>
I have also seen:
<%= form_for #book, :url => {:controller => 'book2s', :action => #action} do |f| %>
<%= f.text_field :title %>
<% end %>
and you just set #action in the controller (probably create or update).
Note that you don't need to include the url_for like you have.
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!
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...
I have a model 'Article' and a model 'Ratings' nested within articles.
/articles/123/ratings
I want to change the routing of the f.submit in ratings/_form.html.erb
now it is so, that after pressing submit, my application routs to
/ratings/111
but I want to route it to
/article/123
How can I change the routing in a form_for f.submit button.
I have found here something like this:
<% form_for :thing, :url =>
url_for(:action => "update", :id => #thing) do |f| %>
But this do not work for my rails 3.2. Thanks for your help,
:url - The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well. Defaults to the current action.
<% form_for :thing, :url => {:action => "update", :id => #thing} do |f| %>
you can also pass it the path_name by using the helper. so you can also do something like
:url => update_article_path(#article)
Try form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}).
I have a form which is always submitting the form to the "update" function in the controller, and I have created this form using "remote_form_for" tag. In this form I have objects from different tables, and from this form I want to submit entire form data to another function(not to the "update" function) via AJAX request.
I have tried many methods including using submit tag with action
<% remote_form_for #employee, :url => organization_employee_path(#organization, #employee), :method => :put do |employee_form| %>
// form objects and other functionalities
....
....
// views inside the forms
<div id="employee_header_div">
<%= render :partial => "employee_header", :locals => {:employee => #employee} %>
</div>
...
...
<%= submit_tag "page_level_validation", :id => "page_level_validation" , :action=>"validate"%>
<% end %>
But the Ajax request always calling the same "update" function.
It would be very helpful, if anyone helps to resolve this issue.
You can't set the submit to point to a different place than the main form has specified (unless you want to use the HTML5 formaction attribute and deal with the browser compatibility consequences).
However, what you could do is create a new action in your controller which deals with the situation.
e.g..
<% remote_form_for #employee, :url => organization_employee_validate_path(#organization, #employee), :method => :put do |employee_form| %>
in your controller
def validate
#do something loosely based around the update method
end
not forgetting to add the appropriate routes.
Try this:
<% form_for #employee, :remote => true, :url => organization_employee_path(#organization, #employee), :method => :put do |employee_form| %>