Search code examples
ruby-on-railsform-helpers

rails form_tag right arguments


okay so i'v searched the net but i couldn't find the help i needed and im not very experienced with the helper tags, so what i'm trying to do is: theres a user so called interface, and there exists a form for user's status, by default its empty and it renders the form, when he inputs the status i want it to be saved and redirected back to his page, or just a page refresh.

the form

<%= form_tag interface_path(@user, username: "username"), {method: :put, class: "form-group"} do %>
    <div class="form-group">
        <%= text_field_tag 'status', :status , class: 'form-control' %>
    </div>
        <%= submit_tag %>
<% end %>

controller

class InterfacesController < ApplicationController
    #before_action :authenticate_user!

    def show
        @user = User.find_by(username: params[:username])
        #@interface = @user.interface
    end

    def update
        @user = User.find_by(username: params[:username])
        redirect_to interface_path(@interface)
    end
end

P.S having trouble with the correct redirection aswell.

routes

Rails.application.routes.draw do
  devise_for :users
root 'posts#index'

#get 'profile/:username', to: 'interface#profile', as: 'profile' 
  resources :interfaces, only: [:show, :update] , param: :username
  #get 'interfaces/:username', 'interfaces#show', as: 'interface'

  resources :posts do 
    resources :comments
  end

and rake routes

                  Prefix Verb   URI Pattern                                 Controller#Action
        new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
            user_session POST   /users/sign_in(.:format)                    devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
           user_password POST   /users/password(.:format)                   devise/passwords#create
       new_user_password GET    /users/password/new(.:format)               devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)              devise/passwords#edit
                         PATCH  /users/password(.:format)                   devise/passwords#update
                         PUT    /users/password(.:format)                   devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                     devise/registrations#cancel
       user_registration POST   /users(.:format)                            devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                    devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                       devise/registrations#edit
                         PATCH  /users(.:format)                            devise/registrations#update
                         PUT    /users(.:format)                            devise/registrations#update
                         DELETE /users(.:format)                            devise/registrations#destroy
                    root GET    /                                           posts#index
               interface GET    /interfaces/:username(.:format)             interfaces#show
                         PATCH  /interfaces/:username(.:format)             interfaces#update
                         PUT    /interfaces/:username(.:format)             interfaces#update
           post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                         POST   /posts/:post_id/comments(.:format)          comments#create
        new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
       edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
            post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                         PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
                         PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                         DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
                   posts GET    /posts(.:format)                            posts#index
                         POST   /posts(.:format)                            posts#create
                new_post GET    /posts/new(.:format)                        posts#new
               edit_post GET    /posts/:id/edit(.:format)                   posts#edit
                    post GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy

btw the form method above dosn't save the status to db.

schema

ActiveRecord::Schema.define(version: 20160312102350) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: :cascade do |t|
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "post_id"
  end

  create_table "interfaces", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username"
    t.text     "avatar"
    t.string   "status"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

Solution

  • Try redirect_to interface_path(@user)

    Where are you declaring @interface?