RoutingError on Ruby Rails? - ruby-on-rails

Hey everyone, thanks for reading this.
Ihave the next issue: When I call my "New" template (generated by scaffold) I got the next error:
<h1>ActionController::RoutingError in Flujos_de_trabajo#new</h1>
Showing app/views/flujos_de_trabajo/new.html.erb where line #3 raised:
flujos_de_trabajo_url failed to generate from {:controller=>"flujos_de_trabajo", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["flujos_de_trabajo", :id] - are they all satisfied?
Extracted source (around line #3):
1: <h1>New flujo_de_trabajo</h1><br/>
2: <br/>
3: <% form_for(#flujo_de_trabajo) do |f| %><br/>
4: <%= f.error_messages %><br/>
5: <br/>
6: <p><br/>
I have overlooked everything, and I don't know what the problem is. The code in the view and in the controller is the same as the generated. In fact, i deleted it, generated it againg, and nothing, the same problem. Can you help me?

Did you do this.
script/generate scaffold FlujosDeTrabajo
rake db:migrate
http://localhost:3000/flujos_de_trabajos/new
it is working for me

Rails is really bad for languages that aren't English. It's failing here because it thinks that "flujo_de_trabajo" is the singular version of "flujo_de_trabajo". You're going to have to set up some inflections telling Rails the correct singular version of this. Look at the examples in config/initializers/inflectors.rb.

Related

Rails: Access model from different controller

I know this question been ask before and they said 'models are independent of the controller.' So, following my same code I did for other models I just renamed my working code to fit to my new model. But I'm getting an error undefined method 'userimages_path' Here the code I'm using.
The model is userimage and the controller is uploads.
Controller/uploads_controller.rb
def new
#userimage = Userimage.new
end
Model/userimage.rb is an empty file
Views/uploads/new.html.erb (This line is throwing the error.)
<%= form_for #userimage do |f|%>
In my routes.rb
resources :uploads
I have rake db:migrate several times to make sure I did migrate the database thinking this might be why it can't find the Userimage.
What I have I done wrong/missing here?
It is a Rails magic, when you don't specify second option(url) for form_for, Rails try to set it, in your case <%= form_for #userimage do |f|%> converts by Rails to <%= form_for #userimage, url: userimages_path do |f|%>, in your routes, there is no such _path helper.
To resolve this issue run bundle rake routes and set the right url option.
Check the documentation
try below code:
view
<%= form_for #userimage, url: "/uploads" do |f|%>
uploads controller
def create
...
end

ruby-on-rails tutorials: Can't show a user's microposts

As I was reading "Ruby on Rails 3 Tutorial", I was trying to figure out what I did wrong in chapter 11 as I couldn't have the test in listing 11.15 to pass.
So here I am, stuck in section 11.2.1. Since I couldn't get anywhere with it, I decided to ignore this and proceed further: to fill up 50 microposts for the first six users and then try to access a user's show page. I got the following error page:
NoMethodError in Users#show
Showing <my_path>/sample_app/app/views/users/show.html.erb where line #10 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #10):
7: </h1>
8: <% unless #user.microposts.empty? %>
9: <table class="microposts" summary="User microposts">
10: <%= render #microposts %>
11: </table>
12: <%= will_paginate #microposts %>
13: <% end %>
With trial and errors, it looks like the instance variable #microposts is nil for some reason. It looked as if the method "UsersController.show" didn't do its job of creating that instance variable. However, I could verify that this method is executed since I could manage to acces a user's show page successfully provided that he doesn't have any micropost.
At this step, it's hard to give more hints. But I can tell that all the other tests are passing.
try replacing all your occurrences of #microposts with #user.microposts.
Is #microposts defined in your controller action? Or is #user defined in your controller action? Because it seems like #user.microposts is probably the same thing as #microposts (from this context) and unless you've declared a variable for an associated user.microposts object, you might be trying to access a nonexistent variable.
could it be #user.microposts? The thing that on line 8 it checks for the existence of?
Well, with these types of errors, there's a stupid mistake creeping somewhere.
Following the advice of the ruby.railstutorial.org site. I did compare my code against the one of the author. Guess what, in my version of UsersController, there was two definitions of the 'show' method! And that second instance, at the bottom of the file, didn't have any assignment to the #microposts instance variable.
Would I had to compile the thing, the bug would have been spotted. I guess that there must be a way to scan a rails site against a ruby syntax checker.
Anyway, thanks for the other people suggestions.

Rails, Making a Rails form to save a record to an external db

Rails 2.3.5
I haven't used Rails in awhile and I'm a bit out of practice. For the application I'm working on there is an external db that's scanned by a running process and creates tickets in a ticket system. All I need to do is just save a record there.
I thought I could just connect the db and use a Rails form where I create a new model object and then a form uses that - where submitting the form should just go to a create action in the controller.
The error I'm getting from trying this has me stumped though (undefined method `tam_ticketings_path').
Thanks for any tips or help. I never had to deal with saving a record to a db outside teh application and I'm not sure exactly what I should be trying to do here (save going back to an HTML form and a manual SQL Insert statment).
Thanks!
database.yml:
tam_ticketing_db:
adapter: mysql
database: tam_ticketing_1
model: tam_ticketing
class TamTicketing < ActiveRecord::Base
TamTicketing.establish_connection "tam_ticketing_db"
set_table_name "tickets"
end
Tickets controller method:
def new_ticket
#ticket = TamTicketing.new
new_ticket view:
<% form_for(#ticket) do |f| %>
<%= f.error_messages %>
the error:
Showing app/views/tickets/new_ticket.html.erb where line #1 raised:
undefined method `tam_ticketings_path' for #<ActionView::Base:0x3b01f18>
Extracted source (around line #1):
1: <% form_for(#ticket) do |f| %>
2: <%= f.error_messages %>
3:
4: <p>
When you use form_for(someModelInstance) it will use the path method that goes to the create/update action. Make sure you have properly routed your TamTicketing model using something like this in your config/routes.rb file
resources :tam_ticketings
In Rails 2.3.5 the config/routes.rb should look like this:
map.resource :tam_ticketing
Then restart/start your server and browse your view again.
Also on your controller the proper naming for your action should just be 'new' and not 'new_tickets' inorder to have the above routing work correctly. Otherwise you need to add this:
map.new_ticket 'tam_ticketings/new_ticket', :controller => 'tam_ticketings', :action => 'new_ticket'
map.resource :tam_ticketing
I suggest making sure your controller is named TamTicketings (file name is tam_ticketings) and the action is 'new'

Rails : Scaffold works for the first, but not for the second table

I am using aptana radrails
empty rail project :
scaffold Article titre:string body:text categorie_id:integer ordre:integer
Migrate -> it works fine
scaffold Categorie titre:string ordre:integer
It generate the files but when i access http://127.0.0.1:3000/categories i have the following error :
NameError in Categories#index
Showing app/views/categories/index.html.erb where line #22 raised:
undefined local variable or method `new_categorie_path' for #
Extracted source (around line #22):
19:
20:
21:
22: <%= link_to 'New categorie', new_categorie_path %>
i deleted recreated my whole rails project a few times, changed categorie with another name but it keep failling. Why ?
Adding to Salil's answer which is correct, Rails convention is to have english like naming.
If you put
Categorie
in your scaffold Rails will not be able to pluralize it correctly in categories.
You can also try a different path using legacy names and no pluralization but it is a painful path.
I would advice to use english naming.
It should be
scaffold Category titre:string ordre:integer

active_scaffold routing error

If you haven't seen my question yesterday, this is my second rails app. The first went nice and smooth, but this one keeps giving me one random error after another. I installed active_scaffold for this app as well as the last app (the first error, instead of using script/install plugin git://active_scaffold repository, I did script/install plugin http://active_scaffold repository.) I didn't want to spell out basic CRUD on minor models. After the install problems, (before I found the http solution from a windows user when I'm on Linux) I thought I'd try out Hobo. Well, Hobo updated actionmailer, actionpack, activerecord, activeresource, and installed rack. Rails isn't even using the updated versions. But as you can see at the bottom of the trace it's using rack. I have a feeling it has something to do with my futzing around with installing Hobo which I uninstalled. Thanks in advance.
[Edit]
I had asked the question over at the
ActiveScaffold Group
the answer (if you don't want to follow the link) was that this line needed to be added to routes:
map.resources :modelName, :active_scaffold => true
It doesn't entirely answer my question, since the documentation said nothing about changing routes. But, it works.
[/Edit]
ActionController::RoutingError in Departments#index
Showing vendor/plugins/active_scaffold/frontends/default/views/_list_header.html.erb where line #8 raised:
No route matches {:_method=>:get, :action=>"show_search", :controller=>"departments"}
Extracted source (around line #8):
5: <% next if controller.respond_to? link.security_method and !controller.send(link.security_method) -%>
6: <% next if link.action == 'new' && params[:nested].nil? && active_scaffold_config.list.always_show_create %>
7: <% next if link.action == 'show_search' && active_scaffold_config.list.always_show_search %>
8: <%= render_action_link(link, new_params) -%>
9: <% end -%>
10:
11: <%= loading_indicator_tag(:action => :table) %>
Trace of template inclusion: vendor/plugins/active_scaffold/frontends/default/views/list.html.erb
Full Trace It was taking forever to format it. I'm still not fully conversant in SO's formatting (sometimes the server is down. reboots are reinstalls. it's a play server)
Add this to routes:
map.resources :modelName, :active_scaffold => true
And, contrary to my edit, it is in the documentation. It's in the wiki at Github. My second question at SO, and I could have found the answer by RTFM'ing. <sigh>
For Rails 4+ at least, your entry in config/routes.rb should look like:
resources :models do # my model name in plural
as_routes
# for action links of type member, add the following
# see also [note, via the action-link API, you can change the HTTP verb. Adjust this route accordingly.
# https://github.com/activescaffold/active_scaffold/wiki/Adding-custom-actions
# get :my_custom_action, :on => :member
end
Note, getting the right version of the Gem requires the following config in your Gemfile:
gem 'active_scaffold', github: 'activescaffold/active_scaffold', :branch => "3-4-stable"
If you get an error that "as_routes" isn't found; that'd be the problem.
THIS IS RAILS 4 ONLY
For Rails 5.x you can set route as follow:
resources :model, concerns: :active_scaffold

Resources