[英]Rails passing hidden form data via Controller
UPDATE: It seems like the error was probably due to a devise conflict. 更新:似乎错误可能是由于设计冲突。 I used some content from another app which apparently made devise go nutty and not be able to read user sessions.
我使用了另一个应用程序中的一些内容,这些内容显然使设计变得难以理解,无法读取用户会话。 I'll be starting with a clean install and seeing if I have better luck.
我将从全新安装开始,看看我是否有更好的运气。 Thanks!
谢谢!
I am creating a page which has a single question on it. 我正在创建一个页面,上面有一个问题。 On that page I have a form where people can answer that question.
在该页面上,我有一个表格,人们可以回答该问题。 The form itself just has an answer block and a submit button.
表单本身只有一个答案框和一个提交按钮。 However, I want to tie it to both the user submitting the form and the question the answer is linked to.
但是,我想将其与提交表单的用户以及答案链接到的问题相关联。 For security I want to do this in the controller, not the view.
为了安全起见,我想在控制器而不是视图中执行此操作。 The answer form is being shown on the questions#index page.
答案表格显示在questions#index页面上。 I am currently getting the following error:
我目前收到以下错误:
undefined method `user' for #<AnswersController:0x007fe618e5cc10>
I suspect that if it made it past the user I would get the same for 'question' 我怀疑,如果它超过了用户,我也会对“问题”有所了解
The questions#index looks like this: Questions#index看起来像这样:
<div class="home_question">
<h1><%= @daily.question %></h1>
<div class="answer_form">
<%= form_for @answer do |answer| %>
<%= answer.label :answer, "What do you think?" %>
<%= answer.text_area :answer %>
<%= answer.button %>
<% end %>
</div>
</div>
The Questions Controller looks like this: 问题控制器看起来像这样:
def index
@daily = Question.find_by(show_month: Time.now.month, show_day: Time.now.day)
@answer = Answer.new
end
The Answers Controller looks like this: Answers控制器如下所示:
def index
@answers = Answer.all
@answer = Answer.new
end
def new
@answer = Answer.new
end
def create
@answer = Answer.new(answer_params)
@user = user(params[:id])
@question = question(params[:id])
if @answer.save
redirect_to root
else
render 'new'
end
end
private
def answer_params
params.require(:answer).permit(:answer, :users_id, :questions_id)
end
The data currently being passed by the form is: 表单当前传递的数据是:
{"utf8"=>"✓",
"authenticity_token"=>"mA16+dg7+Edzxvu/FVWR5r8PZ9zdNaOyvOwSz1VpOXU=",
"answer"=>{"answer"=>"test"},
"button"=>""}
The error that you're getting is because in the controller you're using the user
variable which isn't defined. 您得到的错误是因为在控制器中您正在使用未定义的
user
变量。 You want to use the uppercase model name User
. 您要使用大写的型号名称
User
。
Getting the user id 获取用户ID
If this is a logged in user then you can get their id by looking in the session. 如果这是已登录的用户,则可以通过在会话中查看来获取其ID。 Usually Rails application will have a helper like this:
通常,Rails应用程序将具有如下帮助程序:
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
You don't need this helper if you're using devise -- it's already done for you. 如果您使用的是devise,则不需要此帮助程序-它已经为您完成。
Getting the question id 获取问题ID
First make sure that your associated resources are nested. 首先,请确保您的关联资源是嵌套的。 So in our
routes.rb
file: 因此,在我们的
routes.rb
文件中:
resources :questions do
resources :answers
end
This means that to create an answer we'll need to POST /questions/:id/answers
. 这意味着要创建答案,我们需要
POST /questions/:id/answers
。
The form changes to: 表格更改为:
<%= form_for [@daily, @answer] do |answer| %>
<%= answer.label :answer, "What do you think?" %>
<%= answer.text_area :answer %>
<%= answer.button %>
<% end %>
and in the controller: 并在控制器中:
def create
@answer = Answer.new(answer_params)
@answer.user = current_user
@answer.question = Question.find(params[:id])
if @answer.save
redirect_to root
else
render 'new'
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.