Rails form doesn't submit if page is loaded by link from another page

Using rails 4.2.0.

View: The users/edit.html.erb

view is used for the account settings form hitting the controller / user model. The app uses the Devise app, but the view is devise/registrations.edit.html.erb

used as a form of password to change.

If I go to my account settings page via its URL / users /: id / edit and edit, the form will submit a fine and update the attributes as expected.

Question:

The form is not submitted if you visit the page using a link.

  • I click on the "Account Settings" link on a separate page, <%= link_to "Account settings", edit_user_path(current_user) %>

  • users/edit.html.erb

    view downloads
  • Make and edit an attribute in user / edit view
  • Click the "Submit" button
  • Form not submitting, POST not submitting, button not responding.

Relative routes:

           edit_user GET    /users/:id/edit(.:format)  users#edit
                user GET    /users/:id(.:format)       users#show
                     PATCH  /users/:id(.:format)       users#update
                     PUT    /users/:id(.:format)       users#update

      

Opportunities:

I've never seen this before. Any help / ideas for solutions or how best to debug?

EDIT1:

users/edit.html.erb

:

<% provide(:title, "Account Settings") %>

<div class="row">
  <div class="small-12 columns">
    <h2>Account settings</h2>
  </div>
</div>

<div class="row">
  <%= form_for(@user) do |f| %>
  <%= devise_error_messages! %>
  <div class="small-3 columns">
    <%= gravatar_for(@user, :size => 500) %>
    <%= link_to "Upload", "#", :class => "button tiny expand" %>
  </div>
  <div class="small-8 columns small-offset-1">
    <div class="field">
      <%= f.label :first_name %>
      <%= f.text_field :first_name %>
    </div>
    <div class="field">
      <%= f.label :last_name %>
      <%= f.text_field :last_name %>
    </div>
    <div class="field">
      <%= f.label :email %>
      <%= f.text_field :email %>
    </div>
    <div class="field">
      <%= f.label :profile_description %>
      <%= f.text_field :profile_description %>
    </div>
  </div>
</div>

<div class="row">
  <div class="small-1 columns settings-icons-container">
    <%= image_tag 'icons/facebook.png', class: 'social-settings-icons' %>
    <%= image_tag 'icons/twitter.png', class: 'social-settings-icons' %>
    <%= image_tag 'icons/google-plus.png', class: 'social-settings-icons' %>
  </div>
  <div class="small-4 small-pull-7 columns">
    <h5>Social media links</h5>
    <!-- REPLACE WITH ERB -->
    <input type="text" for="facebook">
    <input type="text" for="twitter">
    <input type="text" for="website">
  </div>
</div>

<div class="row">
  <div class="small-3 columns">
    <%= link_to "Change password", edit_user_registration_path, :class => "button tiny expand radius" %>
  </div>
</div>

<div class="row">
  <div class="small-3 columns actions ">
    <%= f.submit "Save changes", :class => "button expand radius small-3 columns" %>
    <% end %>
  </div>
</div>

      

+3


source to share


3 answers


I had a similar problem: whenever you came to a page with help link_to

, I needed to update the form to submit.

I fixed this by adding data-no-turbolink="true"

inside the body tag application.html.erb

.



Solved by looking "Submit Form" button only works after reboot .

+1


source


It looks like you might want to include a method on the form:

<%= form_for(@user) do |f| %>

      



to

<%= form_for(@user, method: :put) do |f| %>

      

+1


source


This is the answer to this part of the question: Any help / ideas on ... how best to debug?

A useful debugging tool for form problems is opening the page in a browser. Select to view the source of the page and search the "form" string to see exactly what code was generated by the code.

For example, this is the html from one of my forms:

<div class="form-inline">
    <div class="row">
    <form id="search-form" class="form-group" action="/locations" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="xxx" />
    <div class="input-group input-group-sm gx_ml_10 gx_mr_10">
      <input type="text" name="search" id="search" placeholder="Search for BBQ" class="form-control" />
     <span class="input-group-btn input-group-sm">
            <button type="submit" class="btn btn-primary">
                  <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
    </button></span>
   </div> <!-- "input-group" --> 
  </form>     
 </div>  <!-- row -->
</div> <!-- "form-inline" -->

      

Specifically in the html for the section of the form you are looking for:

<button type="submit" ...

      

Without it, you have no action in the form.


Addition / Revision

Another suggestion for form debugging is to use an alert to see the hash at runtime. To highlight the differences between what is passed to a controller with direct URL access versus "link_to" access, examine the hash generated in both cases. Add this statement to the custom controller edit block.

flash[:info] = "Hash: #{params} User: #{@user.some_var_like_name}"

      

+1


source







All Articles