The hobo lifecycle tutorial shows how to implement a friendship logic in
the model and controller. However it does not really cover how to glue
the gui/views together. When I go to /friendships/invite - hobo
presents me with a form with a drop down menu. How do I add a form to
the user show-page with just one button (Invite) I guess that the the
user viewed should be in a hidden field?
I tried adding the form like this:
<extend tag="show-page" for="User">
<old-show-page merge>
<append-content-body:>
<invite-form for="Friendship" />
</append-content-body:>
</old-show-page>
</extend>
Hobo ignores the invite-form hmmm I must be missing something.
Best regards
Asbjørn Morell
try <invite-form:friendships.build/> assuming you have has_many :friendships on your user model. for is an attribute on the 'def' and 'extend' tags, it's not recognized by 'form'. To use a polymorphic tag, simply pass in an instance of the correct type in the context. This of course is easier if an instance already exists, but since you're using a creator action, just build one.
P.S. Questions like this get answered much quicker on the Hobo Users mailing list.
Related
I followed the railscasts nested model form part 1, making some changes to have it work in rails 4. Basically, I created 3 models: Quiz, Question, and Answer, and they all belong_to the model intuitively above them. A form in the new action is used to create the quiz itself.
However, I'm at a bit of loss on how to proceed now. After creating the quiz, the show view looks like this:
done by iterating through #quiz.questions and #quiz.questions.answers and just displaying them on the page with their respective content attributes.
That's great for displaying just the questions and answers, but it doesn't accept user input at all. How do I make it so the user can use radio buttons to select an answer, and have it submit SOMEWHERE to save the results for grading and future reference for the user?
I've actually tried to create a form simply within the show action and have it save to another model but I got stuck extremely quickly. I'm finding it really difficult to both display the results AND accept user input for the displayed results. I also can't figure out a good way to save this data. Making another 3 models with the equivalent of Questions having something like a user_answer attribute seems difficult to implement and messy. I'm a beginner of the grandest caliber so any help would be great!
Nachime, your data model is good so far from what I can tell. Now you will want to link the answers to the users via many-to-many relationship. Note: this assumes that users will only take each quiz once.
Your next data model version will have a users_answers table containing user_id and answer_id. You can access the relative models using the has_many :through association so that each user has_many :answers, through: :users_answers.
The quiz will essentially just link the logged in user to the array of answers selected.
My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I have a collection of records that are related to a specific parent object. I have no need to update the parent, just want to update all the children.
I tried making a Reform::Form and simply adding the collection declaration. An example might be a developer has many projects. When the developer goes on holiday his projects are "on_hold", but some of them might still be active. So, I would like to list all the projects with a checkbox to check if they should be put on hold. I essentially want to have a simple_fields_for :projects.
Does it make sense to use Reform for this?
I have this code:
class ProjectsForm < Reform::Form
collection :projects do
property :name #perhaps I want to rename them in the same form (bear with me)
property :on_hold
end
end
This should work, but when initializing the form with a hash
#form = ProjectsForm.new({projects: #array_of_projects})
I get an error
undefined method `projects' for #<Hash:0x007fce8f2783b8>
As if the collection method is not working. I am obviously doing something stupid.
I'd really like to use Reform. I love the philosophy behind a lot of the trailblazing suit of gems. It will be great if someone can point me in the right direction here.
If it turns out this isn't a good usecase for Reform I'll find another :P
Update:
I think reform is slightly more coupled with the idea of a model than what I thought. I thought it was just a form object with properties to play nicely with form builder. I now find that your model is key. You need to initialize the Reform form with A model, or in the case of composition, a few models. However, if you pass in an array of hash reform believes this is the model and then tries to access the :projects property on the model. In my case a hash.
I have now added a
# my contrived example is getting lame
attr_accessor :projects
to the developer class and it is working. At least the generating the reform object is working.
I am still curious wether this is a good use-case for Reform.
I'm using the gem nested_form by Ryan Bates. I'm a bit confused about the workflow for the user.
I'm supposed to have a "f.link_to_add" call, that will dynamically add a nested structure for the association. That works as advertised, but I feel that it is cumbersome for the user to FIRST click "Add Book" (as in the commonly used Author/Books example) and THEN type in the new Book fields.
I would like to have an empty set of Book fields shown initially, and if the user fills them and clicks Save, the are added to the Author.
How would I use the nested_form gem to achieve this scenario?
Thx
I would suggest checking out build. For example, your new action for your author could have something like:
#author = Author.new
#author.books.build
Note that if an author could only have one book rather than multiple you'd do something like this instead:
#author = Author.new
#author.build_book
I think the short answer is that nested_form is not suitable for this particular workflow.
It's easier achieved with a separate form for the associated class where you in the controller lookup the parent class (Author) and in the Book form keep a hidden field (:author_id in this example).
This only works if you can accept to add Books one by one.
Nested_form is more suitable when you really want to show a long list of Books and let the user have the option of adding/removing multiple items before Saving.
I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly