Wednesday 15 September 2010

ruby on rails - passing extra values into a partial and to a create action through a form -


In my train app, I have an idea / question / show.html.erb page, which expects additional @ questions Variable also reaches an @tars variable to show all the answers of any specific navy.

Inside a loop that displays all the answers to a specific question, I want to display an area to comment for a user (not a question on the answer), so I have a The comment partially made, after the advice of the other SO, I have created a hash for the local people in which the variables have been passed. In the actions of the Controller of Comments, I want to use the answer (a comment is an answer) and the question (which is, to me: Answer), so I answer the question ID in partial and such as

 < Code> & lt;% answer @ tower% & gt; .... (code omit) & lt;% = render: partial = & gt; 'Comment / Form' ,: Local = & gt; {: Answer_id = & gt; Answer.id ,: Question_id = & gt; @ Question.id}%> ... (code omitted) & lt;% end% & gt;   

And I write partial like answer_id and hidden fields in comments by question_id then

 ; pass = lt;% = simple_form_for @comment | F | & Gt%; & Lt;% = f.input: Content, like: text, label: 'comment'% & gt; & Lt;% = f.hidden_field: user_id ,: value = & gt; Current_user.id% & gt; & Lt;% = f.hidden_field: answer_id ,: value = & gt; Answer_id%> & Lt;% = f.hidden_field: question_id ,: value = & gt; Question_ID% & gt; & Lt;% = f.button: Submit% & gt; & Lt;% end% & gt;   

In the actions of the comments controller, I def @question = Question.find (make this parameter

  [: comment] [: question_id] ) @answer = answer.find (parameter [: comment] [: answer_id]) @comment = @ answer.comments.build (parameter [: comment]) If @ comment.save flash [: notice] = "comment made successfully" Redirect_to root_path # redirect_to question_answers_path (@question) (Finally want to redirect to the question) and render: action = & gt; First problem, I think I have done something strange to remove the following question id (and answer id)    

pre> @question = Question.find However, these parameters are available as a result of submitting my form

  (  

Parameters: {"UTF8" => "One ????" "Authenticity_token" = & gt; "VtogfCsI137lbk2l64RXtrfRn / + RT1 / jM8pfDVY29gM =", "Comment" = & gt; {"Content" = & gt; "test", "user_id" = "12", "answer_id" => "25", "question_id" = "gtc:" 22 "}," committed "= >

Then I will have to type this question code such as params [: comment] [: question_id] < P> Second, more challenging problem (challenging for me) is that the rail is telling me

  can not give large-scale protected features: question_id   

I have no reason to store Question_id comment is on the model, etc. So I did not create a column in the database for this. However, when I do this

  @comment = @ answer.comments.build (params [: comment])   

Because one of question_id is Inside parameter: The comment (which is making a hidden field by me), is trying to save it in the Rail Comments table. However, there is only one reason that I really want to use this question in making the action of comment_controller.rb that after saving it it is necessary to use it to redirect the question.

Can you suggest what I can do to fix this problem? I feel that I do not have much experience with the train, I am doing all this in a very strange way, which is probably not working on it. I think this problem is to add a question_add column to the easy-to-use comment model, but there is no 'association' between them, so I think this is a wrong solution.

Your first problem is not a problem There are not too many ways to do this.

The second problem, you have to do this

@comment = @ answer.comments.build (params [: comment] [: content]. Merge (user_id: params [ : Comment] [: user_id], answer_id: params [: comment] [: answer_id]) otherwise you will try to specify the properties that you do not have

or else you Other ways can

  parameters [: comment] .delete ('question_id')   

and then

  @comment = @ answer.comments.build (Params [: Comment])    

No comments:

Post a Comment