可以将表单添加到 rails 的另一个模型视图中 [英] Possible to add a form into another models view in rails

查看:41
本文介绍了可以将表单添加到 rails 的另一个模型视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 中构建了一个非常小的应用程序.这是一个简单的体重跟踪应用程序.我创建了一个 User 模型,它有一个注册页面.用户登录后,他们将被重定向到 user#show 视图.这是到目前为止的用户控制器:

class UsersController <应用控制器before_filter :authenticate_user!高清秀@user = current_user结尾结尾

我有 2 个其他模型,一个是 Weight 模型,另一个是 Goal 模型,我想要它,所以当用户注册时,他们会看到一个屏幕要求他们输入当前体重和目标体重,这些信息将与登录的用户 ID 一起分别存储在 WeightGoal 模型中.

到目前为止,我已经能够向用户 show.html.erb 模板添加表单:

<%= form_for @user do |f|%><%= f.fields_for :weight do |builder|%><字段集><%= f.label :value,当前重量"%<br/><%= f.text_field :value %><br/></fieldset><%结束%><div class="actions"><%= f.submit %>

<%结束%>

它正确地呈现了表单,但是当我点击提交按钮时,它只会转到一个错误页面,上面写着Unknown action - The action'update' could not be found for UsersController.我假设我做错了什么,因为它应该尝试发送到创建操作.

有没有人可以帮助我回到正确的道路上,我对铁路非常陌生.

解决方案

您正在使用 form_for Rails 助手并将 @user 传递给它,因为 @user 是一个持久模型(保存在数据库中)然后生成的表单将使用 PUT 方法对 /users/:id 进行操作,因此请求将被发送到 UsersController 中名为 update 的操作,您似乎没有在 UsersController

中定义该操作

应该是这样的:

def 更新@user = Users.find(params[:id])如果@user.update_attributes(params[:user])# 如果保存成功就做一些事情别的# 如果保存失败做一些事情结尾结尾

I have a very small application I ma building in rails. It is a simple weight tracker app. I have created a User model which has a sign up page. Once the user logs in they are redirected to the user#show view. Here is the user controller so far:

class UsersController < ApplicationController
before_filter :authenticate_user!

def show
  @user = current_user
end

end

I have 2 other models one is a Weight model and the other a Goal model, I would like it so what when a user signs up they are presented with a screen asking them to type in the current weight and goal weight this information will then be store in the Weight and Goal models respectively along with the logged in users ID.

So far I have been able to add a form to the user show.html.erb template :

<%= form_for @user do |f| %>

  <%= f.fields_for :weight do |builder| %>
    <fieldset>
      <%= f.label :value, "Current weight" %><br />
      <%= f.text_field :value %><br />
    </fieldset>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Which renders the form correctly but when I then click on the submit button it simply goes to an error page saying Unknown action- The action 'update' could not be found for UsersController. Im assuming iM doing something wrong as it should try and send to the create action.

Is there anyone out there who could help me back on the right path, Im very much a noob at rails.

解决方案

You are using the form_for Rails helper and passing @user to it, because @user is a persistent model (saved in the db) then the generated form will have the action to /users/:id with PUT method so the request will be sent to an action named update in your UsersController, it seems that you don't have that action defined in your UsersController

it should be somthing like the following:

def update
  @user = Users.find(params[:id])
  if @user.update_attributes(params[:user])
  # do something if saving succeeds
  else
  # do something if saving fails
  end
end

这篇关于可以将表单添加到 rails 的另一个模型视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆