Passing information from an existing record, to a new record, of a different table
If I have Table A
with columns title
and description
, and I want to create a record in Table B
, which also has a title
and description
column, is there a way to pass that information to new_b_path
such that the /views/b/_form.html.erb populates with the data from the A
record?
I am using the below code to clone
a workout
but this is acting within Table A
so to speak. I want to clone
across tables. Any ideas?
workouts_controller.rb
...
def new
@workout_count = Workout.count
if params[:cloned_workout]
workout_to_clone = Workout.find params[:cloned_workout]
@workout = workout_to_clone.clone
else
@workout = current_user.workouts.new
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @workout }
end
end
...
/views/workouts/s开发者_StackOverflowhow.html.erb
<%= link_to "Clone Workout", new_workout_url + "?cloned_workout=#{@workout.id}", :class => "float-right" %>
Hmm. Judging by your explanation it sounds like a better strategy might be to simply create a table called Recommended with two belongs_to relationships that just has a workout_id and user_id column. Then you can associate the recommendation to a user and the workout. You could also have a gym_id or trainer_id column for the gym or trainer that made the recommendation. Make sense?
精彩评论