4
votes

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

4 Answers

11
votes

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.

0
votes

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.

0
votes

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 !

0
votes

If you've come to this page, wondering why your button isn't submitting, check for the following:

  1. That you've got valid Html (the accepted answer).
  2. That 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.)