开发者

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

开发者 https://www.devze.com 2023-03-14 09:02 出处:网络
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 andcreates tickets in a tic

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开发者_如何学Python 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'

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号