jmknoll
jmknoll

Reputation: 1044

Rails form_for submit button not working

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 %>

Upvotes: 5

Views: 17363

Answers (4)

BenKoshy
BenKoshy

Reputation: 35585

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.)

Upvotes: 1

Medyas
Medyas

Reputation: 1

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 !

Upvotes: 0

jmknoll
jmknoll

Reputation: 1044

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.

Upvotes: 11

Nickolay Kondratenko
Nickolay Kondratenko

Reputation: 1951

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.

Upvotes: 0

Related Questions