5

Thank you for your patience. Still pretty new to Rails.

Using Rails 3.2

Making a signup page for a simple app. My problem is that the submit button on my form doesn't cause any effect, whether the information in the form is valid or not.

The User model and database both seem to work fine. If I add a user manually from the rails console, it will add it to the database. As far as I can tell, the issue seems to be in the form generated by form_for.

Here is the page in question:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
    <div class="span6 offset3>
        <%= form_for(@user) do |f| %>
            <%= render 'shared/error_messages' %>

            <%= f.label :name %>
            <%= f.text_field :name %>

            <%= f.label :email %>
            <%= f.text_field :email %>

            <%= f.label :password %>
            <%= f.password_field :password %>

            <%= f.label :password_confirmation, "Confirmation" %>
            <%= f.password_field :password_confirmation %>

            <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
        <% end %>
    </div>
</div>

And here is my users controller:

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
        if @user.save
            flash[:success] = "Thank you for signing up! Please check your email to confirm your account."
            redirect_to @user
        else
            render 'new'
        end
    end
end

Submit doesn't cause any type of error - simply doesn't cause anything to happen at all. So no valuable information in the logs.

Thank you in advance for any insight.

EDIT: Adding code from /shared/_error_messages.html.erb partial

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li>* <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

4 Answers 4

11

Well there's a nice little lesson in careful coding:

<div class="row">
    <div class="span6 offset3>
        <%= form_for(@user) do |f| %>

should have been:

<div class="row">
    <div class="span6 offset3**"**>
        <%= form_for(@user) do |f| %>

Quite a difference a closing tag can make.

Sign up to request clarification or add additional context in comments.

1 Comment

Same issue, totally different code. Lesson: If buttons in forms are not working, then someone forgot a closing > somewhere.
1

Other Reasons why your form is not submitting:

Check that:

  1. ...you've got valid Html (the accepted answer).
  2. ...you don't have a required: true in your form (that's hidden) and is not showing up (this took two painful hours of my life to discover - hopefully it was not spent in vain.)

Comments

0

I think the issue is that you don't pass errors to your shared/error_messages partial. You have to pass @user there I think. But I'm not sure because I didn't see the shared/error_messages partial. I'd like to get it to give you more detailed answer.

3 Comments

Thanks @nickolay. I've added the text from the shared/error_messages partial. I've called <% if @user.errors.any? %> in the partial. Is there something else I need to do to make the instance variable available in this view?
@knolljm, try smth like the following: stackoverflow.com/questions/10531762/…
I think all the validations are working fine, as in the console I'm able to save valid users, and invalid users are rejected. The machinery works fine under the hood. I think the issue is likely in the form itself or in the button used to submit the form, but I don't have any ideas beyond this.
0

Replace this line : @user = User.new(params[:user]) in your controller by this one: @user = User.new(user_params)

Always in your controller, the methode users_params should be like:

def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
end

it worked well for me !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.