public function formFor(Rails\ActiveRecord\Base $model, $attrs, \Closure $block = null) { if ($attrs instanceof \Closure) { $block = $attrs; $attrs = []; } if (!isset($attrs['html'])) { $attrs['html'] = []; } if (!isset($attrs['url'])) { // if (Rails::config()->ar2) { $className = get_class($model); if (($primaryKey = $className::table()->primaryKey()) && $model->getAttribute($primaryKey)) { $action = 'update'; } else { $action = 'create'; } // } else { // $action = $model->id ? 'update' : 'create'; // } $attrs['url'] = ['#' . $action]; } else { $token = new UrlToken($attrs['url'][0]); $action = $token->action(); } $html_attrs = $attrs['html']; if (!isset($html_attrs['method'])) { if ($action == 'create') { $html_attrs['method'] = 'post'; } elseif ($action == 'destroy') { $html_attrs['method'] = 'delete'; } else { $html_attrs['method'] = 'put'; } } # Check special attribute 'multipart'. if (!empty($html_attrs['multipart'])) { $html_attrs['enctype'] = 'multipart/form-data'; unset($html_attrs['multipart']); } if ($html_attrs['method'] != 'post') { $method = $html_attrs['method']; $html_attrs['method'] = 'post'; } else { $method = 'post'; } $url_token = new UrlToken($attrs['url'][0]); if ($url_token->action() == 'create') { $action_url = Rails::application()->router()->urlFor($url_token->token()); } else { list($route, $action_url) = Rails::application()->router()->url_helpers()->find_route_for_token($url_token->token(), $model); } $html_attrs['action'] = $action_url; ob_start(); if ($method != 'post') { echo $this->hiddenFieldTag('_method', $method, ['id' => '']); } $block(new \Rails\ActionView\FormBuilder($this, $model)); return $this->contentTag('form', ob_get_clean(), $html_attrs); }
/** * Parses the "to" property, which is actually a UrlToken. * This isn't necessary to do if the route is like ':controller/:action'. */ private function _parse_token() { if (!$this->_controller || !$this->_action) { if (!$this->_action && isset($this->_vars['action'])) { $this->_action = $this->_vars['action']['value']; } if (!$this->_to) { if (!isset($this->_vars['controller']) || !isset($this->_vars['action'])) { return false; } $this->_controller = $this->_vars['controller']['value']; $this->_action = $this->_vars['action']['value']; } else { $to = $this->_has_variable_to() ? $this->_replace_variable_to($this->_controller, $this->_action) : $this->_to; try { $token = new UrlToken($to); list($this->_controller, $this->_action) = $token->parts(); } catch (Exception $e) { return false; } } } return true; }