【问题标题】:Rails - Passing parameters from a view to a controllerRails - 将参数从视图传递到控制器
【发布时间】:2015-07-29 20:27:20
【问题描述】:

我正在尝试在我的网站上实现“设置”页面。在此页面中,用户可以更改一些基本设置,例如他们的电子邮件和订阅电子邮件。现在,我只是想让他们能够退订电子邮件。首次注册时,他们可以选择选中复选框,该值存储在 mysql 数据库中。现在,我很难弄清楚如何从不同的页面更改它。 以下是注册控制器的外观:

class SignUpsController < ApplicationController

  def index    
  end

  def new
    @user = SignUp.new
  end

  def cost
    return 10
  end

  def create

    @user = SignUp.new(sign_ups_params)

    if @user.save!
      UserMailer.welcome_email(@user).deliver

      flash[:notice] = "Thank you " + @user.first_name + ", account created     successfully."
      redirect_to(:controller => 'home')

    else
      flash[:error] = "something failed"
      redirect_to :back
    end
  rescue
    flash[:error] = "Failed to create user, email is taken or passwords do     not match"
    redirect_to :back
   end

  private
    def sign_ups_params
  params.require(:sign_ups).permit(:first_name, :last_name, :email,     :password, :password_confirmation, :subscription)
    end
end

这是注册页面视图的一部分,显示了我是如何填写和提交表单的:

<h3>Create New User</h3>

    <%= form_for(:sign_ups, :url => {:action => 'create'}) do |f| %>

    <table summary="Subject form fields">
      <tr>
        <th>First Name</th>
        <td><%= f.text_field(:first_name) %></td>
      </tr>
      <tr>
        <th>Last Name</th>
        <td><%= f.text_field(:last_name) %></td>
      </tr>
      <tr>
        <th>Email</th>
        <td><%= f.email_field(:email) %></td>

      </tr>
      <tr>
        <th>Password</th>
        <td><%= f.password_field(:password) %></td>
      </tr>
      <tr>
        <th>Password Conformation</th>
        <td><%= f.password_field(:password_confirmation) %></td>
      </tr>
      <tr>
        <th>Subscribe to email</th>
        <td><%= f.check_box(:subscription) %></td>
      </tr>

    </table>
    <input class="btn btn-primary" name="commit" type="submit" value="Create User"/>
  </div>

我已经为设置页面制作了一个“个人资料”视图和控制器,但不确定这是否是正确的做法,或者我是否应该只使用注册控制器。 提前谢谢!

【问题讨论】:

  • 您是否正在寻找仅更新某些特定用户信息的方法?
  • @FrancescoPirrone 是的,我是

标签: html ruby-on-rails ruby model-view-controller


【解决方案1】:

您可以并且应该使用您的 signups_controller,因为您希望让用户能够编辑其注册记录(或注册)。

您只需要添加一些东西来完成这项工作。首先,在控制器中创建 editupdate 操作:

def edit
  @user = SignUp.find(params[:id])
end

def update
  @user = SignUp.find(params[:id])
  respond_to do |format|
    if @user.update(sign_up_params)
      # do this
    else
      # do that
    end
  end
end

还将您的 sign_ups_params 重命名为单数形式 => sign_up_params

以及你对应的观点

views/sign_ups/edit.html.erb
<%= render 'form' %>

views/sign_ups/_form.html.erb
<%= form_for(@user, :url => {:action => 'update'}) do |f| %>
....

因为您的 config/routes.rb 中可能有 resources :sign_ups,所以您应该很高兴。


SignUpProfile 分开可能会使生活变得更加艰难,除非您以非常干净的方式使用像devise 这样的宝石。

您要更新哪个对象,或者哪个对象包含您要更新的属性?

如果您想更新sign_up_params,请在您的SignUpsController 中执行此操作,并根据您收到的错误提示添加您的update 操作。 但是,如果您需要更新 profile_params,请在您的 ProfilesController 中执行此操作。

现在您可能会看到混乱。考虑一下自己查看别人的代码,看到 ProfilesControllerSignUpsController create 操作。

为什么不只拥有一个 ProfilesController,其中 sign_up 是 Profile 对象的创建操作?

如果您将其保留在您的 SignUpsController 中,请将 editupdate 移动到那里。

【讨论】:

  • 澄清一下,我应该在注册控制器或配置文件控制器中添加编辑和更新方法吗?因为我尝试将它们添加到 SignUp 控制器,但是当我创建表单时,我收到错误消息“找不到 ProfileController 的操作'更新'。”
  • 我明白你的意思。我想更新 sign_up_params。我是否应该在 Profile 视图中通过执行 form_for(:sign_ups, :url => {:action => 'update'}) 来引用 SignUpsController,并在 SignUpsController 中添加 'update' 方法?然后我就不需要 ProfileController。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 2014-12-01
相关资源
最近更新 更多