rails 3 observe_form - ruby-on-rails-3

I'm upgrading rails_2 project to rails_3. Have a problem with loading data into view from :(
old environtment: rails 2.3.2, ruby 1.8.7
now : rails 3.2.11, ruby 1.9.3
I have a form_for #lead and observe_form in view
{ :action => :update_price }, :frequency => 0.1 %>
but when I run rails server, I see empty page in browser :(

<%= form_for %> Have you added "=" in the form tag??? – Sachin R

Related

How to create user with Authlogic in db/seed.rb?

When I use the following line in db/seed.rb:
#Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
User.create!({:name => "debug", :email => "debug#deb.com", :password => "12345678", :password_confirmation => "12345678"})
I get:
/authlogic-3.1.3/lib/authlogic/controller_adapters/abstract_adapter.rb:30:in `params': undefined method `params' for main:Object (NoMethodError)
from /authlogic-3.1.3/lib/authlogic/session/params.rb:96:in `params_credentials'
from /authlogic-3.1.3/lib/authlogic/session/params.rb:72:in `params_enabled?'
from /authlogic-3.1.3/lib/authlogic/session/params.rb:66:in `persist_by_params'
[...]
I have the same error with this command in the console.
Yet everything is fine in the application itself, i only have this issue in db/seed.rb or console.
The first commented line makes no difference (source http://www.tatvartha.com/2009/09/working-with-authlogic-in-scriptconsole/).
Ruby 1.9.3
Rails 3.2.8
Authlogic 3.1.3
Auto-answer:
I had to remove the following from config/environment.rb:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

rails 3, paperclip assigns id 0 when uploading image

iam using rails with formtastic for my admin backend. i want to be able to upload an image to my recordset, and i try to use paperclip to to that.
when i edit a recordset, the upload of the image works just fine. when i try to CREATE a NEW recordset, paperclip seems to assign the ID 0 for that image in my upload path!
#expected path for new image:
/public/logos/2342/some_image.png
#and thats what i get when i create my new record-set:
/public/logos/0/some_image.png
i tried to add attr_accessible to my model
attr_accessible :logo_file_name, :logo_content_type, :logo_file_size, :logo_updated_at
but that throws me an sql-error
Column 'logo_file_size' cannot be null
EDIT: solved the mysql error when i add attr_accessible. i just allowed the logo_file_size to be null. but the id=0 problem still exists...
my code:
MODEL:
has_attached_file :logo,
:url => "/:class/:attachment/:id/:basename.:extension",
:styles => { :original => ["150x150>", :png] }
VIEW:
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :logo, :as => :file %>
<%= f.input :link, :as => :url %>
<%= f.input :published, :published => 'Veröffentlicht' %>
<% end %>
CONTROLLER:
def create
Article.create(params[:article])
end
my datebase has these 4 colums in Article-Table:
logo_file_name
logo_content_type
logo_file_size
logo_updated_at
iam using rails 3.1.1, formtastic 1.2.4, paperclip 2.4.5
thanks a lot for your help!!!
i know its a bit late, but i found the problem and will share the answer for everyone with the same problem.
problem was mysql, upgraded to mysql2 gem, and everything worked as expected

Is view_context no longer available in Rails 3 views?

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.

Delete link doesn't work but button does

I have a problem very similar to this one: rails 3 - link_to to destroy not working
But delete/destroy links do not work; I simply get redirected to the show page of the object. When I make a button for delete, it all works fine. But I'd like to understand why. Does anyone know?
I seems to be related to some .js files I am using/calling.
<!-- This link doesn't work -->
<%= link_to('Delete', post, :confirm => 'Are you sure?', :method => :delete) %>-->
<!-- This button does work -->
<%= button_to "delete", post, :method=>:delete, :class=>:destroy, :confirm => 'Are you sure?' %>
Post Controller
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
UPDATE
After doing some further research it seem that everyone else having a similiar issue has included the following jquery library:
<script type="text/javascript" src="/javascripts/dragdrop.js"></script>
But I still don't know what the issue is...
LOG
Started GET "/posts/7" for 127.0.0.1 at Tue Jul 12 08:34:06 -0400 2011
Processing by PostsController#show as HTML
Parameters: {"id"=>"7"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 7 LIMIT 1
SQL (0.2ms) SELECT COUNT(*) FROM "comments" WHERE ("comments".post_id = 7)
Rendered posts/show.html.erb within layouts/application (116.5ms)
HTML generated
Delete
UPDATE: I've found that removing <script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script> fixes my problem. But I don't understand why. Is there a known conflict between jquery1.5.1 and rails 3.0.7?
Make sure you include these in your application layout:
<%= javascript_include_tag(:defaults) %>
<%= csrf_meta_tag %>
In my case adding
<%= javascript_include_tag(:defaults) %>
did not work. However explicitly defining java script files did the trick
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'application' %>
Not sure yet why :defaults tag didn't work...
Rails will automatically load jquery for you if you have jquery-rails gem loaded via your Gemfile (this is the default rails configuration). It is then loaded via:
<%= javascript_include_tag(:defaults) %>
Have a look at app/assets/javascripts/application.js for the code that tells rails to add jquery via assets:
//= require jquery
//= require jquery_ujs
By trying to load another copy of jquery via this line:
<script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script>
You have caused a clash of jquery instances, the effect of which will be that neither will work.

Simple Rails 3 Route is Failing - No route matches

In my config/routes.rb I have:
post "portal_plan_document/update"
rake routes confirms this:
$ rake routes
portal_plan_document_update POST /portal_plan_document/update(.:format) {:controller=>"portal_plan_document", :action=>"update"}
....
In my code I have:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:method => "POST", :remote => true do |f| %>
In my log file I see:
Started POST "/portal_plan_document/update" for 127.0.0.1 at 2011-03-31 18:04:37 -0400
ActionController::RoutingError (No route matches "/portal_plan_document/update"):
I am lost as what to do from here. Any help would be greatly appreciated!
I should state I am using Ruby 1.9.2 and Rails 3.0.5. Oh, I have restarted the server (WebBrick w/rails server) after updating routes.rb.
Jeremy
Figured it out! :)
if you have a non-empty object, rails assumes you want to update that object. i.e., use a 'PUT' instead of a 'POST'
to accomplish 'PUTs', rails will put a hidden input in the form, with "_method" = "put". so, it LOOKS like it's a POST, but rails is treating it as a PUT.
if you actually want to just update an object (what it looks like you're doing), a PUT is better, and you should just switch your routes over to PUT.
if (like I was), you're doing something that really requires a POST (i.e., it can't be sent more than once safely), you can write your form_for like this:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:html=>{:method => "POST"}, :remote => true do |f| %>
to confirm, look at the generated HTML source, and make sure the hidden "_method" field is not set to "put"
Try using that instead please :
:method => :post
If this does not still work, please lose the remote attribute and give it a try. Does it work without it ?
I had the same problem while upgrading a simple app from Rails 2 to Rails 3.
As you may guess I was updating all "remote_form_for(#item) (..)" helpers to "form_for :item remote => true (..)" syntax.
In my case this code from a items/_new.html.erb partial:
<%= form_for :item, :remote => true do |f| %>
<!--FIELDS-->
<% end %>
Gave me this error:
Started POST "/items/new" for 127.0.0.1 at Fri Aug 12 18:19:23
+0200 2011
ActionController::RoutingError (No route matches "/items/new")
As you can notice the method was a correct "POST", not a "PUT". The problem lied in the routing... I don't know why but when a remote POST method is sent by a partial, Rails routes the POST request to "/items/new" instead of "/items" route. Even if the purpose is to create a new "item" so the POST request should be correctly (and RESTfully) routed to "/items".
This code, with explicit action and controller, solved the problem:
<%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" } do |f| %>
<!--FIELDS-->
<% end %>