I have the following view that used to render without any warnings:
#listing
-if flash[:notice]
.success
=flash[:notice]
.input-container
-form_for #user do |f|
=f.error_messages
=render :partial => 'form', :locals => {:f => f}
But now when I render the view by running a functional test, I get the following warning:
DEPRECATION WARNING: - style block
helpers are deprecated. Please use =.
Does anyone know what this warning means?
Yeah, instead of:
-form_for #user do |f|
use
=form_for #user do |f|
In other words, do exactly what it suggests. Flip the dash into an equals. This is new in Rails 3.
http://edgeguides.rubyonrails.org/3_0_release_notes.html#action-view (Section 7.4.2)
Related
Rails has generated this action url for my form:
<form action="/auth/failure?action=update&controller=users"...
I'm authenticated to the website, even If I shouldn't be, it would redirect somewhere as I understand Auth.
My Form code:
<%= form_for #user, :url => { 'controller' => 'users', 'action' => 'update'} do |f| %>
Thanks!
There is two thing you have to know
Ruby makes difference between symbols (:controller, :update) and strings ('controller', 'update').
You do not need to specify :url in this case.
So, for your current code, the correct line is simple:
form_for #user do |f|
If #user is a new record, form_for will point to create action of your UsersController, if #user is an existing record, form_for will point to update action automatically. Rails is smart enough to do this :-)
If you really want to use :url attribute of form_for, you have two option:
Use routing helper methods: form_for #user, :url => user_path(#user), :method => :put
Use a correct path hash: form_for #user, :url => { :controller => :users, :method => :update, :id => #user.id }
Rails uses REST style for building urls if you use resources :users in config/route.rb (and I recommend you to use that), and it have two thing you need to know:
- Collection is a group of entities (in your case, users)
- Member is one entity
So collection URL is something where you can expect multiple entities, member URL is something where you can expect only one entity.
You must use :id when you describe update action with URL-hash, because update can done only on member, not on collection. So you have to build a member URL with a special HTTP method (PUT) to clarify, what do you want to do.
in a current Rails 3.0.9 app of mine I had a few .js.erb templates that were using view_context in them so I could call fields_for on it during a ajax request. This was letting me build some nested attribute form fields via ajax. But upon upgrading to Rails 3.1 I'm getting the follow error:
ActionView::Template::Error (undefined local variable or method `view_context' for #<#:0x1057b9f70>):
Was this removed/deprecated recently? Is there another way I can build nested fields_for inputs without having the parent FormBuilder handy? It seems view_context is still available in the controller, but I was hoping to keep this markup generation in the View layer.
My .js.erb template looked like this
<% meal_item_fields = view_context.fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end
%>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
According to api docs it is deprecated in >= 3. Source of 3.0.9 returned self for view_context. I think if you were to try without view_context it would just work.
<% meal_item_fields = fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end %>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
You might want to add helper_method :view_context in your controller.
OK, so Ive set up my mailer in Rails which works fine, but I wanted to make a new action (or maybe just a view?) to have a slimmed down contact form in a lightbox. I can do that all fine and dandy but it would use the default layout which I dont want. So I added:
render :layout => 'lightbox'
to the action so that I could use a new layout. Unfortunately that seems to block off my access to the model as I get this error when the lightbox pops up
undefined method `model_name' for NilClass:Class
#on this line
<% form_for #contact, :url => {:action => "create"}, :html => {:method => :post} do |f| %>
So by using a different layout I cant use the resources I set up in my routes which is here:
resources :contacts, :only => [:new, :create], :as => :contacts
#Im passing in a name to the email form
match "contacts/direct/:name" => "contacts#direct", :as => :direct_email
I hope that made sense. But what do I do?
I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"
I have the following routes:
resources :categories do
resources :articles
end
And the following views:
# edit.erb and new.erb files:
<%= render :partial => 'form' %>
# top of _form.html.erb file:
<%= form_for category_article_path(#article.category, #article) do |f| %>
But I have some troubles with the given path. I work with Rails 3. Here is an example of error that I get when testing:
undefined method `category' for
nil:NilClass
What is the basic way to write a such path? Many thanks.
Just pass a freshly newed up article (with an existing category) instance to the view.