Forum: Ruby on Rails How to send data from a form to the model for update?

7a096d30179ceb77d76d65c0eddc1d98?d=identicon&s=25 Charlie Lor (chuck)
on 2016-09-28 13:35
I have a page that contains multiple tabs. In the movies_controller of
the Movie tab I have this:

def update
@tab = params[:tab] || ''
if @tab.blank?
throw 'no tab given to MoviesController::update()'
elsif !tabs.keys.include?(@tab)
throw "tab '#{params[:tab]}' not supported in
MoviesController::update()"
else # it's a valid tab
# get the valid params for the given tab:
safeparams = send("#{@tab}_params")

# try to update the location with the params:
movie = Movie.find(params[:id])

movie.update(safeparams)
end
end

def actor_params
# params.require(:location).permit :address_ids
params.require(:movie).permit(actors[:first_name, :last_name, :role,
:bio])
end

And then in the movie.rb model, I have this.

def actors=(actors)
actors.each { |a|
 Actors.update(a)
}
end

And in my actor.html.erb

<%=form_for @movie do |l|%>
<% @movie.actors.each do |f| %>
<p>
<label for="first_name">First Name</label>
<input type="text" name="movie[actors][<%=f.id%>][first_name]"
id="first_name" value="<%=f.first_name%>"/>
</p>
<p>
<label for="last_name">Last Name</label>
<input type="text" name="movie[actors][<%=f.id%>][last_name]"
id="last_name" value="<%=f.last_name%>"/>
</p>
<p>
<label for="bio">Role</label>
<input type="text" name="movie[actors][<%=f.id%>][bio]" id="role"
value="<%=f.role%>"/>
</p>
<p>
<label for="bio">Bio</label>
<input type="text" name="movie[actors][<%=f.id%>][bio]" id="bio"
value="<%=f.bio%>"/>
</p>
<% end %>
<footer>
<a class="button" href="/movies">Back to movies</a>
<a class="action button" href="#" onclick="ajtabs.save();return
false;">Save</a>
</footer>
<%end%>

In the actor.html.erb, how do I allow the user the click a +add button
to dynamically add a new actor form for enter more than one actor at a
time?

In the movie_controller/actor_params, how do I pass an array of all the
actors from the actor.html.erb page to the movie.rb model?

In the movie.rb model actors method, how do I access the Actor.rb model
to update each actor in the array that's being passed?

I'm not sure if I make any sense here but basically what I'm trying to
do is updating the actor.html.erb form tab to the Actors table. Any help
is much appreciated.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.