Jump to content

The ultimate community for Ruby on Rails developers.


Photo

controller/form_for for another model


  • Please log in to reply
1 reply to this topic

#1 swigrid

swigrid

    Passenger

  • Members
  • 9 posts

Posted 18 April 2014 - 08:20 AM

Hello,

 

I'm trying to have whole controller containing simple_form/form_for managing another model.

 

this is what i have now:

 

Model -> user.rb

Controller -> system/employees_controller.rb

 

User model is created by Devise gem and I would like to handle it with my own controller. I can handle users from console. Updating them, creating new users, changing their passwords ok, that's why I though i would have spare controller for handling this and I would like to keep simple_form so I can keep design same.

 

my controller:

class System::EmployeesController < ApplicationController
  # ….

  # GET /system/employees/new
  def new
    @system_employee = User.new
  end

  # ….
end

my form:

<%= simple_form_for @system_employee do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">

    <%= f.input :email %>
    # …

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

and error I'm getting:

 

Showing .../app/views/system/employees/_form.html.erb where line #1 raised:

undefined method `users_path' for #<#<Class:0x007fb49cef2678>:0x007fb49a616ba0>

Extracted source (around line #1):

1
2
3
4
<%= simple_form_for @system_employee do |f| %>
<%= f.error_notification %>
 
<div class="form-inputs">

 

thanks



#2 swigrid

swigrid

    Passenger

  • Members
  • 9 posts

Posted 18 April 2014 - 08:41 AM   Best Answer

hello,

 

as soon as i posted qustion i found my solution. I was struggling with for two days, it isnt that I was searching first and asked first here.

 

anyway, here is how it's working for me:

 

only i had to change is form statement for new and edit view

 

first one is for new and the second is for edit

<%= simple_form_for @system_employee, url: system_employees_path, html: { method: :post } do |f| %>
  <%= f.error_notification %>

<%= render 'form', f: f %>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<%= simple_form_for @system_employee, url: system_employee_path(@system_employee), html: { method: :put } do |f| %>

  <%= f.error_notification %>

<%= render 'form', f: f %>


  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users