Creating a Model in Rails within the controller of a different Model

Chang Liu

I'm trying to implement a quote saving feature in a Rails app. I have a User Model that has_many :quotes and a Quote Model that belongs_to :user. Now I have a separate Book Model that would be the source of these quotes.

Within the Book's show.html.erb file, I have a form to save quotes for the current user

<%= form_for (@new_quote) do |f| %>
<div class="field">
    <%= f.hidden_field :book_id, :value => @new_comment.book_id %>
    <%= f.text_field :body %>
</div>
<div class="actions">
    <%= f.submit %>
</div>
<% end %>

And in the Book controller, I have

def show
    @new_quote = current_user.quotes.create(book_id: params[:id])
end

The quote saves fine but the problem is, since I have this Quote creation statement in the show method, everytime I go to the show.html.erb page of my Book model, it creates a Quote with an empty body.

What can I do to solve this? I was thinking it probably would involve moving this Quote creation to the actual create method of the Quote controller but I don't know how to exactly pass the parameters through.

spickermann

You could just build that quote, but not save it to the database. Then the user need to send the form to save that record. Just change your show method to:

def show
  @new_quote = current_user.quotes.build(book_id: params[:id])
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rails: Creating new model based on a parameter in another model's controller

Rails: Calling methods in a model in a different controller

How to use scope of a model, in a different model's controller in rails 4

Creating model with rails

Creating instance of model in controller returns nil for id and timestamp Rails 6

Model method in Rails controller

Rails - retrieve result of method call from controller in a different model

Ruby on Rails: finding a model instance from a different controller

Creating an independent form for a model that belongs to a different model?

Rails mailer working on controller but not on model

Rails 4 scope model/controller

Fat model skinny controller in Rails

Rails - Calling a model method in controller

Creating a rails migration and and populated existing values based off of a method within a model

How to display params from a different Model within the /show of a different Model?

Rails model validation in different context

Same Model with different columns Rails

Accessing different model elements in Rails

creating controller keeps model from getting to template

Model as dependency property used within different threads

Rails - Create instance of a model from within another model

Rails - Create instance from a model within another model

Django creating different tables for same model

Rails skip validation within model with save?

Find category within category model - Rails

Creating Join Table in Rails with One Model and Two Attributes on Another Model

Rails: Nested model form not creating record

Spring error when creating a new model in Rails

codeigniter - use model results in controller to call a different function in the model