I have a triple nested resource, which I am able to create new values for perfectly well. However when trying to edit the record, I am getting duplicated fields for the nested values, which is then creating multiple entries.
I am multiplying the nested fields by 3.
def new
#roast = Roast.new
3.times {#roast.countries.build.regions.build}
end
Edit method:
def edit
#roast = Roast.friendly.find(params[:id])
3.times {#roast.countries.build.regions.build}
end
Should I be removing the 'build' element here? I do want the user to be able to add new values if required however.
And create has nothing special for this:
def create
#roast = Roast.new(roast_params)
respond_to do |format|
if #roast.save
format.html { redirect_to #roast, notice: 'Roast was successfully created.' }
format.json { render :show, status: :created, location: #roast }
else
format.html { render :new }
format.json { render json: #roast.errors, status: :unprocessable_entity }
end
end
end
I obviously want the 3 nested fields to show on the edit page, but what am I doing wrong for it to keep repeating.
You don't need to add countries or regions in the edit. Just find the Roast.
#This is wrong
3.times {#roast.countries.build.regions.build}
When you edit a Roast, you can access its countries through #roast.countries
If you want, you can define an instance variable to use in the form (although not needed) #countries = #roast.countries
Related
I have two models, Livestock and History
a livestock has many histories and history belongs to livestock
This is the create method inside the LivestockController
# POST /livestocks
# POST /livestocks.json
def create
#livestock = Livestock.new(livestock_params.permit!)
respond_to do |format|
if #livestock.save
format.html { redirect_to #livestock }
flash[:success] = "Livestock was successfully created"
format.json { render :show, status: :created, location: #livestock }
else
format.html { render :new }
format.json { render json: #livestock.errors, status: :unprocessable_entity }
end
end
end
I wanted to create a record in the histories table with
history = History.new(livestock_id: #livestock.id, event: "Purchased", event_date: #livestock.purchase_date, image: #livestock.image)
history.save!
inside the create method
How can I do it? I can't put it in the create method because it says
Validation failed: Livestock must exist
apparently #livestock has not yet have the id attribute
Edit:
it still raises the same exception when I put it after
if #livestock.save
However I found a work around by using the session variable. Since it is redirected to the show page, I created the following inside the create method
session[:created] = "created"
And in my show method
# GET /livestocks/1
# GET /livestocks/1.json
def show
if session[:created] == "created"
history = History.new(livestock_id: params[:id], event: "Purchased", event_date: #livestock.purchase_date, image: #livestock.image)
history.save!
session.delete(:created)
end
end
Now I am wondering what are consequences if I use this approach.
Livestock record is created when you call save (and there is no validation error). So one option is to create the history inside this if condition:
if #livestock.save
Another option is to use after_create callback in the livestock model that will create a history object right after creating livestock. You have to be careful because the callback might be called when you do not need it (i.e. when importing data).
The last option is to create a separate service object that will create livestock and all other required objects. That's probably the best approach, but it will require more customized code.
Update
Please also make sure to move if/else block outside the respond_to block:
if #livestock.save
# create history object here
respond_to do |format|
format.html { redirect_to #livestock }
flash[:success] = "Livestock was successfully created"
format.json { render :show, status: :created, location: #livestock}
end
else
respond_to do |format|
format.html { render :new }
format.json { render json: #livestock.errors, status: :unprocessable_entity }
end
end
I am trying to use the standard create method created for Ruby/Rails projects and simply pass in an additional form field that tells the method how many objects to create (vs just creating one object). The standard create method looks like so:
def create
#micropost = Micropost.new(micropost_params)
respond_to do |format|
if #micropost.save
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
I want to pass in an additional data (form field called number_to_create) which tells the method how many of the microposts to create. I just added a new form field like this, in addition to the other micropost form field params:
<%= text_field_tag :number_to_create %>
My question is how do I modify the create method code such that it creates N number of micropost objects vs. just one. So if I pass in 3 from the form along with the other micropost attributes, the method creates 3 identical micropost objects, not just one as it currently does.
Thanks in advance for your help on this.
You could use the param as times
#microposts = Micropost.transaction do
[].tap do |microposts|
param[:number_to_create].times do
microposts << Micropost.create(micropost_params)
end
end
end
respond_to do |format|
if #microposts.all? &:persisted?
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
The transaction block is to make sure that either all of them gets saved, or none of them gets saved, this way you can fix your errors and recreate them without worrying of getting any stray saved objects
When i update my revisor_id from my Petition model, i want to automatically update my revisor_id from my Post model.
So, in my petitions_controller.rb i added this line within the update method:
#petition.post.revisor_id = #petition.revisor_id
With the debugger i can see that after the line is executed, both are set correctly. But it seems like it's not saved in the database or something, because when i request to show all the posts with revisor_id set to 1, it doesnt show anything.
I believe it has something to do with strong parameters since i just chaged to rails 4, but not sure.
Any idea?
#petitions_controller.rb
...
def update
respond_to do |format|
if #petition.update(petition_params)
#petition.post.revisor_id = #petition.revisor_id
format.html { redirect_to #petition, notice: 'Petition was successfully updated.' }
format.json { render :show, status: :ok, location: #petition }
else
format.html { render :edit }
format.json { render json: #petition.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_petition
#petition = Petition.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def petition_params
params.require(:petition).permit(:user_id, :category_id, :revisor_id, :status, :post_id)
end
You're not calling save.
#petition.post.update_attibute(:revisor_id, #petition.revisor_id)
Or
#petition.post.revisor_id = #petition.revisor_id
#petition.post.save
But it looks like you could redesign your database and you has_one through so you wouldn't need to do that.
I am using almost the code from the regular scaffold. The only change is the 4.times block where I make 4 answer objects on the question. The reason is that I have the corresponding input fields in the view. Now, if the validation fails it renders the new.html.erb again, however after what I have been reading it does not invoke the "new" action again. However I am depending on the 4.times block because otherwise the loop in the view have no answers to loop through. How do I fix this? I tried redirecting but then the error messages disappered.
New action
def new
#question = Question.new
4.times do
#question.answers.build
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: #question }
end
end
Create action
def create
#question = Question.new(params[:question])
respond_to do |format|
if #question.save
format.html { redirect_to #question, notice: 'Question was successfully created.' }
format.json { render json: #question, status: :created, location: #question }
else
format.html { render action: "new" }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
Exactly as your title suggests: render does just render (the template belonging to) the action and not perform the action itself.
To get your prebuilt answers rendered again in a failed create call, I suggest removing the :reject_if condition from the nested attributes setup. This way, empty submitted answers are preserved. To prevent them from being written to the database, just add regular validations to the Question model...
What you need to look at is the #question in your create action. In theory it should contain the 4 newly built answers so redisplaying the form would also contain these.
If they are not written you may have to look at the accepts_nested_attributes_for to make sure it gets deserialized correctly from the request.
I have a validation for the Activity model and this is the create action in the activities_controller. When I try to save the model without meeting the validations, an extra activity instance is being created. Why is an activity being created when it fails the validations?
def create
#activity_last = Activity.find(params[:activity_id])
#activity = #activity_last.dup
#activity.activity_date = Time.now
respond_to do |format|
if #activity.update_attributes(params[:activity])
format.html { redirect_to #activity.tenant, notice: 'Activity was successfully created.' }
format.json { render json: #activity.tenant, status: :created, location: #activity.tenant }
else
format.html { render action: "new" }
format.json { render json: #activity.errors, status: :unprocessable_entity }
end
end
end
The reason I am using dup here is because I want to create a duplicate of the latest activity and then update a subset of its attributes from the params. So for explanation sake, say Activity had 10 attributes. The user would go to a form that has field for 3 of these attributes. I then want to create a new activity that has all the same attribute values as the latest activity for the attributes not being updated by the user.
--Update--
I have uncovered why this was happening. I shortened the code above so that there would be less clutter but as part of that shortening I cut out the following lines:
#comment = Comment.new(params[:comment])
#comment.user = current_user
#comment.activity = #activity
#comment.save!
The app works correctly when I move the saving of the comment to after the saving of the activity. My guess is that saving a comment that is linked to an activity that hasn't been saved causes the activity to be saved?