Add data to table through another
First of all: I have 4 tables:
1) respondents
with id
and email
.
2) inquires
with id, question_id, respondent_id
3) question_id
with id
and text
4) answers
with inquiry_id
, text
What i want? I want to do next: in my view I have question
and text_field
and button
.
If user something answer on question and press button, this data inserts into answer
table.
my_controller (asnwer_controller)
def create
if request.post?
Answer.create(:inquiry_id=>@inquiry.id.to_s, :text=>params开发者_开发技巧[:text])
end
end
my_view
<% form_for :answer, :url => { :controller => 'answer', :action => 'create' } do |f| %>
<%= @questions.id %>. <%= @questions.text %><br />
<%= f.text_area :text, :rols => 10, :cols => 60 %><br />
<%= submit_tag "Send Survey ", :class => "inputBox" %>
<% end %>
I through i something missed in view & controller. Data not pasting now. Please help!
If i understand the question, you want to add data through an intermediate table. So, if a user has many posts, you want to add a post to a user, right ? If user has many posts and you create a new post, you can do it like :
current_user.posts.create(...)
or
current_user.posts << post
Hope that is what you are looking for. If not, please specify a bit more clearly.
精彩评论