rails form_tag右参数

时间:2016-03-12 20:49:28

标签: ruby-on-rails form-helpers

好吧所以我在网上搜索但我找不到我需要的帮助,我对助手标签的经验不是很充分,所以我要做的是:用户所谓的界面,那里存在一个用户状态的表单,默认情况下为空并呈现表单,当他输入状态时我希望将其保存并重定向回他的页面,或只是页面刷新。

表格

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

控制器

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还没有正确的重定向问题。

路由

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

和rake路线

                  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

顺便说一下上面的表单方法不能将状态保存到db。

模式

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

2 个答案:

答案 0 :(得分:0)

尝试redirect_to interface_path(@user)

你在哪里宣布@interface?

答案 1 :(得分:0)

在你的控制器中,我看不到你在保存状态。使用导轨4,您可能需要允许强大的参数strong params。假设状态是用户模型的一部分

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])
    if @user.update(interface_params)
      redirect_to interface_path(username: params[:username])
    end
end

private 
  def interface_params
    params.permit(:username, :status)
  end