Search code examples
ruby-on-railssimple-formsimple-form-for

When I click submit button in my form, create action is supposed to work but it is rather processed by new action


In my application, I have a form that is supposed to be processed by create action but it is rather processed by new action. This problem is happening in my post task form, but in my other form (registration form), it is working very well (it is processed by create action). So I thought maybe the culprit is in my model relationships so I will post it here as well:

User model

has_many :client_relationships, class_name: "Task", foreign_key: "client_id", dependent: :destroy

has_many :worker_relationships, class_name: "Task", foreign_key: "worker_id", dependent: :destroy

has_many :worker_tasks, through: :client_relationships, source: :worker

has_many :client_tasks, through: :worker_relationships, source: :client

Task model

belongs_to :client, class_name: "User"

belongs_to :worker, class_name: "User"

In my routes.rb

resources :users do
  member do
   get :client_tasks, :worker_tasks
   end
  end

resources :tasks

If you are wondering, I have two users in tasks table (client_id, worker_id). I have a boolean column in users table to identify them. I also have appointment table which belongs to task but I did not include it here.

Now, after clicking the submit button, Im getting this from cmd:

Processing by TasksController#new as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"e5K/I8TpQJwpqGtapZs7xP4n7i3FF
a9xHVSv7CLVYIsYSomK95x6B+J2hSbr77CHvxI01te8hQ7HOGRTRLANNg==", "task"=>{"category
_id"=>"", "title"=>"", "description"=>"", "pay_type"=>"", "pay_offer"=>"", "coun
ty_id"=>"", "area_id"=>"", "appointments_attributes"=>{"0"=>{"start_date"=>"", "
start_time"=>"", "end_date"=>"", "end_time"=>""}}}, "commit"=>"Create Task"}
ty_id"=>"", "area_id"=>"", "appointments_attributes"=>{"0"=>{"start_date"=>"", "
start_time"=>"", "end_date"=>"", "end_time"=>""}}}, "commit"=>"Create Task"}

and im getting this on the browser:

http://localhost:3000/tasks/new?utf8=%E2%9C%93&authenticity_token=e5K%2FI8TpQJwpqGtapZs7xP4n7i3FFa9xHVSv7CLVYIsYSomK95x6B%2BJ2hSbr77CHvxI01te8hQ7HOGRTRLANNg%3D%3D&task%5Bcategory_id%5D=&task%5Btitle%5D=&task%5Bdescription%5D=&task%5Bpay_type%5D=&task%5Bpay_offer%5D=&task%5Bcounty_id%5D=&task%5Barea_id%5D=&task%5Bappointments_attributes%5D%5B0%5D%5Bstart_date%5D=&task%5Bappointments_attributes%5D%5B0%5D%5Bstart_time%5D=&task%5Bappointments_attributes%5D%5B0%5D%5Bend_date%5D=&task%5Bappointments_attributes%5D%5B0%5D%5Bend_time%5D=&commit=Create+Task

and on the debug message (last 3 lines):

commit: Create Task controller: tasks action: new

It is supposed to be "action: create" but it is not.

It is really weird, Ive been banging my head all day for this and still can not solve this. I am using simple_form btw. Please help me... I am a newbie here :(

In registration form

<%= simple_form_for @user, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
        <%= f.input :first_name %>
        <%= f.input :last_name %>
        <%= f.input :email %>
        <%= f.input :password %>
        <%= f.input :password_confirmation %>
        <%= f.input :county_id %>
        <%= f.input :area_id %>
        <%= f.button :submit %>
        <% end %>

In post task

<%= simple_form_for @task, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>

        <%= field_set_tag '<span class="label label-info">1</span> What task do you require?'.html_safe do %>
        <%= f.input :category_id, collection: Category.order(:name), label_method: :name, value_method: :id, label: "Category", include_blank: "Select a category of task" %>
        <%= f.input :subcategory_id, collection: Category.order(:name), as: :grouped_select, group_method: :subcategories %>
        <% end %>

        <%= field_set_tag '<span class="label label-info">2</span> Tell us about the task.'.html_safe do %>
        <%= f.input :title, placeholder: 'Give a descriptive title for your task'%>
        <%= f.input :description, placeholder: 'Describe your task here...' %>
        <% end %>

        <%= field_set_tag '<span class="label label-info">3</span> What budget do you have in mind?'.html_safe do %>
        <ul class="nav nav-tabs" id="payTypeTabs">
            <%= f.input :pay_type, as: :hidden %>
            <li><a href="#fixed-price-tab" data-toggle="tab">Hire for a fixed price</a></li>
            <li><a href="#per-hour-tab" data-toggle="tab">Hire per hour</a></li>
        </ul>
        <h1></h1>

        <%= f.input :pay_offer, wrapper: :horizontal_input_group do %>
        <span class="input-group-addon">€</span>
        <%= f.input_field :pay_offer, as: :integer, class: "form-control" %>
        <span class="input-group-addon">Euro</span>
        <% end %>

        <% end %>

        <%= field_set_tag '<span class="label label-info">4</span> Where will your task take place?'.html_safe do %>    
        <%= f.input :county_id, collection: County.order(:name), label_method: :name, value_method: :id, label: "County", include_blank: "Select a category of task" %>
        <%= f.input :area_id, collection: County.order(:name), as: :grouped_select, group_method: :areas %>

        <% end %>

        <%= field_set_tag '<span class="label label-info">5</span> Set an initial appointment for this task.'.html_safe, :id => "date_time" do %>
        <% f.simple_fields_for :appointments do |b| %>


        <div class="row">

            <div class="col-md-5 col-md-offset-2"><%= b.input :start_date, label: "Start", wrapper: :horizontal_input_group do %>

                <%= b.input_field :start_date, placeholder: 'Start date', class: "form-control date start" %><span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>


                <% end %>
            </div>

            <div class="col-md-5"><%= b.input :start_time, label: false, wrapper: :horizontal_input_group do %>


                <%= b.input_field :start_time, placeholder: 'Start time', as: :string, class: "form-control time start" %><span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>

                <% end %>
            </div>
        </div>

        <div class="row">
            <div class="col-md-5 col-md-offset-2">
                <%= b.input :end_date, label: "End", wrapper: :horizontal_input_group do %>
                <%= b.input_field :end_date, placeholder: 'End date', class: "form-control date end" %>
                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                <% end %>
            </div>
            <div class="col-md-5">
                <%= b.input :end_time, label: false, wrapper: :horizontal_input_group do %>

                <%= b.input_field :end_time, placeholder: 'End time', as: :string, class: "form-control time end" %>
                <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
                <% end %>
            </div>

            <% end %>
            <% end %>

            <%= f.button :submit %>
            <% end %>

Solution

  • It is really weird that it works in the first case and not in the second.

    Try this,

    <%= simple_form_for @task, url: tasks_path, method: :post, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
    

    this will force the form to submit to tasks_path with post method which goes to the create action. Let me know if it works

    Also, this is your create method

            def create 
                @task = current_user.client_relationships.build(task_params) 
                if @task.save 
                    flash[:success] = "Micropost created!" 
                    redirect_to @current_user 
                else 
                    render 'new' 
                end 
            end
    

    A couple of issues.

    1) redirect_to @current_user does not mean anything, right ? should be like user_path(@current_user)

    2) you should always do redirect_to and return so that the code block after redirection will not be called. In your case, its ok since the rest is in a else block

    3) It could be that task.save fails and new is rendered. use a debugger and check if comes to first line of create