带有form_tag的回形针,没有相应的型号

时间:2016-01-20 05:50:41

标签: ruby-on-rails activerecord paperclip

我有一张包含要上传图片的表单,但此表单并非直接在模型上构建,我使用form_tag代替form_for

我想上传图片并用Paperclip保存,但是如何让Paperclip从我的表单中理解params?

@user.avatar.picture = params[:my_file_field]

2 个答案:

答案 0 :(得分:2)

要更正您的某些术语,Paperclip 附加到模型,否则您将无法保存您拥有的文件。

你遇到的问题是你正在尝试填充一个与你的路由结构不对应的模型(IE你只传递 图片属性,需要一种方法来解析它的曲别针)。

你仍然会有一个控制器/动作&模型填充...

#config/routes.rb
resources :users, path: "profile", only: [:index] do 
   patch :avatar, on: :member #-> url.com/profile/:id/avatar
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def avatar
       @user = User.find params[:id]
       @user.update avatar_params
   end

   private

   def avatar_params
       params.require(:user).permit(avatar_attributes: [:picture])
   end
end

#app/views/users/show.html.erb
<%= form_for @user, url: user_avatar_path(@user) do %>
   <%= f.fields_for :avatar do |a| %>
      <%= a.file_field :picture %>
   <% end %>
   <%= submit_tag %>
<% end %>

-

如果你想坚持form_tag,你需要删除模型级别的参数:

#config/routes.rb
resources :users, path: "profile", only: [:index] do
   patch :avatar, on: :member
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def avatar
      @user = User.find params[:id]
      @user.avatar.update avatar_params
   end

   private

   def avatar_params
      params.permit(:picture)
   end
end

#app/views/users/show.html.erb
<%= form_tag users_avatar_path(@user) do %>
   <%= file_field_tag :picture %>
   <%= submit_tag %>
<% end %>

几点:

  1. form_tag不会显示错误
  2. 您的file_field需要与paperclip附件
  3. 的名称相同

答案 1 :(得分:1)

你能否对这个问题有更多的描述。我明白你对form_tag

有疑问

如果您使用的是form_tag,则应使用正确的form_tag格式。

    public void searchData()
    {

        string sql = "Select * from Inventory";
        cmd = new OleDbCommand(sql, con);

        try
        {
            con.Open();
            cmd.Connection.CreateCommand();
            string value = cboFields.Text;
            switch (value)
            {
                case "ID":
                    cmd.CommandText = "Select * from Inventory where ID LIKE @searchKey";
                    break;
                case "Quantity":
                    cmd.CommandText = "Select * from Inventory where Quantity LIKE @searchKey";
                    break;
                case "Unit":
                    cmd.CommandText = "Select * from Inventory where Unit LIKE @searchKey";
                    break;
                case "ItemCode":
                    cmd.CommandText = "Select * from Inventory where ItemCode LIKE @searchKey";
                    break;
                case "ItemName":
                    cmd.CommandText = "Select * from Inventory where ItemName LIKE @searchKey";
                    break;
                case "Cbm":
                    cmd.CommandText = "Select * from Inventory where Cbm LIKE @searchKey";
                    break;
                case "TotalCbm":
                    cmd.CommandText = "Select * from Inventory where TotalCbm LIKE @searchKey";
                    break;
                case "":
                    cmd.CommandText = "Select * from Inventory";
                    MessageBox.Show("Select fields where you want to searchData for");
                    txtSearch.SelectionStart = 0;
                    txtSearch.SelectionLength = txtSearch.Text.Length;
                    break;       
            }

            cmd.Parameters.AddWithValue("@searchKey", "%" + txtSearch.Text.ToString() + "%");
            OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
            adap.Fill(dt);
            DGVinventory.DataSource = dt;


            con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            con.Close();

        }


    }
例如,你必须这样做

form_tag(controller: "people", action: "search", method: "get", class: "nifty_form")