Rails 4 - 输入模型

时间:2014-09-03 18:50:04

标签: ruby-on-rails

我正在尝试在市场应用上创建卖家资料。我找不到用户错误。

我在路线中传递了ID,所以我不确定为什么不读它。

路线:

get '/users/:id/sellerprofile' => 'users#sellerprofile', as:'sellerprofile'

users_controller方法:错误在user_params方法中 - “param not found:user”

  def sellerprofile
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to root_url, notice: 'Your profile was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

  def user_params
    params.require(:user).permit(:bankaccname, :profileimage, :profilestory)
  end

sellerprofile.html.erb:

<%= form_for @user, url: user_path, html: { method: :put, :multipart => true } do |f| %>

  <div class="form-group">
    <%= f.label :your_story %><i> (required)</i>
    <%= f.text_area :profilestory, class:"form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :profile_image %><i> (required)</i>
    <%= f.file_field :profileimage, class:"form-control" %>
  </div>

   <div class="form-group">
        <%= f.submit class:"btn btn-primary" %>
   </div>

 <% end %>

以下是服务器错误:

Processing by UsersController#sellerprofile as HTML
  Parameters: {"id"=>"71"}
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT
1  [["id", "71"]]
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 71 ORDER
 BY "users"."id" ASC LIMIT 1
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT
1  [["id", 71]]
Completed 400 Bad Request in 6ms

ActionController::ParameterMissing (param not found: user):
  app/controllers/users_controller.rb:54:in `user_params'
  app/controllers/users_controller.rb:32:in `block in sellerprofile'
  app/controllers/users_controller.rb:31:in `sellerprofile'

1 个答案:

答案 0 :(得分:0)

如果你有以下路线

,你的做法是错误的
match "/users/:id/sellerprofile", to: "users#sellerprofile", via: [:get, :put], as: :sellerprofile

并且您的表单位于sellerprofile.html.erb中,然后您的代码是正确的,除了更新用户部分。

def sellerprofile
    @user = User.find(params[:id])
    if params[:user]
      respond_to do |format|
        if @user.update(user_params)
           format.html { redirect_to root_url, notice: 'Your profile was successfully updated.' }
        else
          format.html { render action: 'edit' }
        end
      end
    end
end

def user_params
    params.require(:user).permit(:bankaccname, :profileimage, :profilestory)
end

您的表单(sellerprofile.html.erb):

<%= form_for @user, url: sellerprofile_path(@user), method: :put, html: { :multipart => true } do |f| %>

  <div class="form-group">
    <%= f.label :your_story %><i> (required)</i>
    <%= f.text_area :profilestory, class:"form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :profile_image %><i> (required)</i>
    <%= f.file_field :profileimage, class:"form-control" %>
  </div>

   <div class="form-group">
        <%= f.submit class:"btn btn-primary" %>
   </div>

 <% end %>