Rails 3 Rspec form_for Deprecation Warning - ruby-on-rails

I have a form_for in my application which looks like:
<%= form_for :user, #user, :url => { :controller => 'users', :action => 'update' }, :html => { :multipart => true, :method => 'put' } do |f| %>
This works fine. However, when I run my tests with Rspec I always get:
DEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead.
Which seems to go against what is written in the Rails 3 forms guide. Is this just a bug in rspec or is it actually a deprecation?

You should just be able to do this
<%= form_for #user, :html => {:multipart => true} %>
Since Rails will know that #user is an existing record, it'll know to do a PUT request to users_controller#update.

Related

Disable enforce utf8 in a redmine form

I have a redmine installation running fine. I have an issue to disable the utf-8 enforcement tag when doing searches. This is because on my network there is a url policy check that will prevent me from opening the url containing the utf-8 enforcement parameter.
So I went to /usr/share/redmine/app/views/issues/index.html.erb and changed this line:
<%= form_tag({ :controller => 'issues', :action => 'index', :project_id => #project },
:method => :get, :id => 'query_form') do %>
to this
<%= form_tag({ :controller => 'issues', :action => 'index', :project_id => #project },
:method => :get, :id => 'query_form', :enforce_utf8 => false) do %>
but the utf8 enforcement parameter shows up
What am I doing wrong? Can I disable it on a global base?

How do I update create route from rails 3 to 4

I have a form tag which is seen below, when I try to run the page I get the error: No route matches {:action=>"create", :type=>"new", :controller=>"lists"}
<%= form_for #list, :url => {:action => "create", :type => "new"}, :html => {:multipart => true,:role=>"form"} do |f| %>
In my routes file i have a line-- resources :lists
I thought the line above in the routes file is supposed to create correct routes for me.
Can someone tell me what I am doing wrong?
The url option needs a controller if you're going to format things this way... and there isn't a type option in url_for... so it should probably look like this:
<%= form_for #list, :url => {:controller => 'lists', :action => "create"}, :html => {:multipart => true,:role=>"form"} do |f| %>
Or without the hash rockets:
<%= form_for #list, url: { controller: 'lists', action: "create" }, html: { multipart: true, role: "form"} do |f| %>
check the method, create must be sent via POST as
<%= form_for #list, as: :list :url => lists_path, method: :post do |f| %>

Rails 3.1 and Paperclip unexplained route problem in production mode

Please help, I don't know what to try anymore....
On my machine everything works fine. But on production box when hitting submit, something is cutting route.
Command path for paperclip in environment is set properly.
This is what I get in production.log when submitting upload form:
Started POST "/app_medias/1/media_image"
ActionController::RoutingError (No route matches [POST] "/1/media_image"):
/app_medias/ gone from the route wtf???
Gemfile
gem 'paperclip', :git => "http://github.com/thoughtbot/paperclip.git"
route:
resources :app_medias do
member do
post 'media_image' #upload action
end
end
rake routes:
media_image_app_media POST | /app_medias/:id/media_image(.:format) | {:controller=>"app_medias", :action=>"media_image"}
controller:
def show
#media = AppMedia.find(params[:id])
#media_image = #media.media_images.build
end
def media_image
#media = AppMedia.find(params[:id])
#media_image = #media.media_images.build(params[:media_image])
if #media_image.save
flash[:notice] = "Saved"
redirect_to app_media_path(#media)
else
flash[:error] = "Error"
render :action => 'show'
end
end
model
has_attached_file :media_image,
:styles => {
:thumb => "50x50#",
:small => "x110",
:original => "x600" } ,
:url => ("/assets/_media/:attachment/:id_:style_:basename.:extension").downcase,
:path => (":rails_root/public/assets/_media/:attachment/:id_:style_:basename.:extension").downcase
view:
<%= form_for :media_image, :as => #media_image, :url => {:action => "media_image"}, :html => {:multipart => true} do |f| %>
<%= f.file_field :media_image %><%= f.submit 'Upload', :disable_with => 'uploading...', :class => 'button primary' %>
<%- end -%>
I tried also form_for #media_image, :url => {:action => "media_image} ... localy works, not in production.
Can somebody help? I'm stuck...
Thank you
Solved! ModSecurity is the problem. Now is off, and upload is working fine.

Additional Parameter in form_for in Rails

Is it possible to submit another parameter outside the form data in rails? My problem is that I render different forms for different classes and send them to the same create method. I would like to send the class with the form (as value not as key in the hash).
Something like the :type parameter (that actually doesn't work)
<%= form_for(#an_object, :url => { :controller => :a_controller, :action => :create },
:type => #an_object.class.to_s.underscore) do |f| %>
The post message looks like:
{"commit"=>"Create Class of an Object",
"authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=",
"utf8"=>"✓",
"class_of_an_object"=>{"name"=>"a name",
"description"=>"a description"}}
and I would have a "type" => "class_of_an_object", but directly in the hash an not within the "class_of_an_object" hash.
<%= form_for #an_object,
:url => { :controller => :a_controller,
:action => :create,
:type => #an_object.class.to_s.underscore } do |f| %>
And I prefer to use named routes
<%= form_for #object, :url => object_path(#object, :type => "whtever"), :html => {:method => :post} do |f| %>
This works for me:
<%= form_for #foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>

Rails: Passing multiple parameters to form_for url?

This works great:
- form_for #user, :url => { :action => :create, :type => #type } do |f| ...
Returns /users/(id)?type=type
But in another view I need to pass TWO parameters into the URL string, and this does not work:
- form_for #user, :url => { :action => :update, :type => #type, :this => #currently_editing } do |f| ...
Returns /users/(id)?this=currently_editing
I've also tried:
- form_for #user, :url => { :action => :update, :params = params.merge({:this => #currently_editing, :type = #type})} do |f| ...
... with no luck (error: only GET requests allowed).
What I want is for it to return this: /users/(id)?this=currently_editing&type=type
Thoughts?
Why do you need to pass them into the URL string? Why not just add them as hidden fields in the form? In almost all cases you should pass the variables that way with POSTs.
I would use hidden fields, but this should work:
<% form_for #user, :url => user_path(#user.id, :type => #type, :this => #currently_editing), :method => :put do |f| -%>
:method => :put triggers the update action when using RESTful routes.
please try to this
you can pass more than one parameter in this way.
- form_for #user, :url => xxx_yyy_path(:param1 => value1, :params2 => value2, ......) do |f| ...
I think you have to move the desired querystring attributes outside of the :url option like this:
form_for #user, :url => { :action => :update }, :type => #type, :this => #currently_editing do |f|

Resources