This might be a simple question, but I can't figure it out.
Model Question has many Model Answer, and they both belong to Model User.
Here is part of index.erb.html:
<tbody>
<% #questions.each do |question| %>
<tr>
<td><%= question.content %></td>
<%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
<td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% if question.answers %>
<h4>Answers</h4>
<% #question.answers.each do |answer| %>
<p>
<%= answer.content %>
</p>
<% end %>
<% end %>
<br>
<p>Add Answers</p>ww
<%= form_for([question,question.answers.build]) do |f| %>
<%= f.text_area :content %>
<%= submit_tag "Add" %>
<br>
<% end %>
</tr>
<% end %>
</tbody>
And I used seed.rb to insert data.
Here is seed.rb
User.destroy_all
Question.destroy_all
Answer.destroy_all
user = User.create(email: "123#gmail.com", password: "2wsx1qaz")
q = Question.create(content: "Q1", user: user)
#q2 = Question.create(content: "Q2", user: user)
Answer.create(content: "answer1", question: q, user: user)
Answer.create(content: "answer2", question: q, user: user)
But it threw this error
I also put this project on github
Update
After above error, the render result is weird.
It should show Question, and then check there are any answer for that question. If there are, shows answers.
But, the result on my browser is like that
But, I expected it should be like this
You don't have #question defined, so it's nil and that's why you got: undefined method answers for nil:NilClass when you called this:
#question.answers
You actually have question defined. So, use that :-)
Change this:
<% #question.answers.each do |answer| %>
To:
<% question.answers.each do |answer| %>
I think this line is incorrect
<%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>
which is outside table data so put this in your <td> clause. an your answers are also not in any table data so put it in proper format of table.
for more info to render table in html #table. so it is disturbed your UI part.
Related
Im trying to create inputs by looping each product and have one form submission for all the inputs. Currently, the form only submits the last input. How would I make it so all the inputs get submitted?
<%= form_for :inventory do |f| %>
<% #products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.measurement %></td>
<td><%= f.number_field :amount, class: 'form-control' %></td>
<%= f.hidden_field :product_id, :value => product.id %>
</tr>
<% end %>
<%= f.submit %>
<% end %>
I dont know your use case but you can use the gem cocoon for doing that. There will also be a link to add/remove another product.
https://github.com/nathanvda/cocoon
You can iterate with each_with_index:
<%= form_for :inventory do |f| %>
<% #products.each_with_index do |product, i| %>
<tr>
<%= f.hidden_field "product_id[#{i}]", :value => product.id %>
</tr>
<% end %>
<%= f.submit %>
<% end %>
Like that the output is not very elegant (something like "product_id"=>{"0"=>"1", "1"=>"2", "2"=>"3"... }) but it's for the example to show how every hidden field needs a unique key.
You can define your params in a better way to use them in the controller, just keep them unique.
I'm trying to create a form that allows a user to select different choices for each category on the same form, and each of these will create its own model. My problem is getting the form to submit multiple parameters. My code is as follows:
<%= simple_form_for choices_path do |f| %>
<td><%= person.fullname %></td>
<td><%= person.email %></td>
<td><%= person.phone %></td>
<% if Category.any? %>
<%= f.simple_fields_for :choices do |g| %>
<% Category.all.each do |category| %>
<td>
<%= g.input :item_id, as: :select, collection: booking.venue.menu_items(category) %>
<%= g.hidden_field :admin_id, value: person.id %>
</td>
<% end %>
<% end %>
<% end %>
<td><%= person.completed? %></td>
<td><%= f.submit %></td>
<% end %>
The idea obviously being to create a new form for each category. This works until submitting the data, where only the last form's data is submitted. My paramaters are as follows:
{"utf8"=>"✓", "authenticity_token"=>"tf3DSLGHyOVEVJkcLjSVE9HDJbjn1CIaYBJIfHFw6RYH8tcn0tNilWVIjzyvcjXYm2ovKNO5A31+TktGA8X2+Q==",
"/choices"=>{"choices"=>{"item_id"=>"Dessert 1", "admin_id"=>"3"}},
"commit"=>"Save /choices",
"venue_id"=>"1",
"booking_id"=>"26"}
I can see that its creating a hash and is ready to submit multiple choices, however it is only the last record's data that is submitted.
Any help on how to get multiple records to update is greatly appreciated.
Thanks in advance.
The simple form association helper might be what you are looking for:
link
So I have a users model which is associated to activities and activities are associated to a completed model?.
What I am trying to do is update if an activity is completed when a checkbox is clicked.
Here is my view:
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
This all works great and shows if an activity is "finished" but I am having a hard time figuring out how to update the database with the checkbox. While I would like it to update when you click the checkbox I am ok with having a save button to save them all at once. Which ever would be easier probably would be best for me as I am just learning rails.
Thank you for any help.
Edit:
To clarify my question, my code above works for showing the data correctly, what I can't figure out is how to turn it into a form so that I can save any changes to the check box values.
The easiest solution would be to use a form and a save button. If you look at the Form Helpers Rails Guide, you can see an example:
<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
You probably want to use form_for, not form_tag though to update your object. See the documentation here.
<%= form_for task do |f| %>
<%= f.label :finished %>:
<%= f.check_box :finished %><br />
<%= f.submit %>
<% end %>
EDIT:
To update multiples, try something like this:
<% form_tag edit_multiple_products_path do %>
<table>
<tbody>
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Update Tasks" %>
<% end %>
Except don't forget to do everything else that the RailsCast mentions.
I am using authlogic for users to login and once they do so, I would like them to be able to add posts. For some reason I keep getting "Couldn't find User without an ID". Below is my code:
posts controller
def create
#user = User.find(params[:user_id])
#post = #user.posts.create(params[:post])
redirect_to current_user_path(#current_user)
end
show page
<% #user.posts.each do |post| %>
<tr>
<td><%= post.postName %></td>
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %> </td>
</tr>
<% end %>
</table>
<br />
<h2>Add a Post:</h2>
<%= form_for([#current_user, #user.posts.build]) do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I know it has something to do with current user, but I cannot get this working! Any help is greatly appreciated!
First, you should use current_user not #current_user
Second, you're doing #user = User.find(params[:user_id]) but I don't understand why you expect to have something like :user_id in your params. That's why you get that "Couldn't find User without an ID" error.
How do I use association data from within a fields_for?
I've got a has many through relationship between users, schools and schools_users. A user can have multiple schools and a school has multiple users. In schools_users there is a user_role field in addition to the common user_id and school_id.
In the show view I have:
<% #school.schools_users.each do |user| %>
<tr><td><%= user.user.first_name %> <%= user.user.last_name %></td><td><%= user.user_role %></td></tr>
<% end %>
but I can't figure out how to do the same thing from within the fields_for on the edit page.
<%= f.fields_for :schools_users do |f| %>
<tr>
<td><%= NEED USER NAME HERE %></td>
<td><%= f.select(:user_role, active_role_options)%></td>
<td><%= link_to_remove_fields 'Delete', f %></td>
</tr>
<% end %>
Thanks for any help!
Firstly: You're assigning the block variable of your fields_for to f, which is the same f your form_for uses. You should use a different name, like user_fields.
With that:
<%= f.fields_for :schools_users do |user_fields| %>
<tr>
<td><%= user_fields.text_field :first_name %></td>
...
<% end %>