How to pass url to partial/form?

2019-09-17 01:27发布

问题:

I would like to pass the url to a form, which is in a partial. However, the current setup generates an error message:

SyntaxError in OrganizationsController#new
syntax error, unexpected keyword_do, expecting keyword_end ...or(@organization), url="url" do |f| @output_buffer.safe_appe... ... 

The error highlights the 3rd line of the partial/form which is <%= form_for(@organization), url="url" do |f| %>

Two views both use the partial/form, and to this end include:

## View1:
<%= render 'registrationform', local:{url: signup_checkout_path} %>
## View2 (url should point to `def create` in organizations controller):
<%= render 'registrationform', local:{url: organizations_path} %>

Routes includes:

resources :organizations
post 'signup/register'    => 'organizations#checkout', as: 'signup_checkout'

And the partial registrationform includes:

<% if local_assigns.has_key? :url %>
  <%= form_for(@organization), url="url" do |f| %>
  ...
<% else %>
  ????
<% end %>

Def new in the controller:

def new
  if (logged_in?)
    flash[:danger] = "You're already logged in"
    redirect_to root_url
  end
  @organization = Organization.new
  @member = @organization.members.build
end

回答1:

Change local to locals

## View1:
<%= render "registrationform", { url: signup_checkout_path } %>
## View2 (url should point to `def create` in organizations controller):
<%= render "registrationform", { url: organizations_path } %>

And pass url variable not "url" string

<% if local_assigns.has_key? :url %>
   <%= form_for @organization, url: url do |f| %>
   ...
<% else %>
   ????
<% end %>