spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Rails deeply nested resources alternative to accessing params


You shouldn't need to directly access the params to create your nested records. For example, if in your event#new action, you may have something like

@event = Event.new

3.times do
  question = @event.questions.build
  5.times do
    question.answers.build
  end
end

This will create a new Event and take you to the New page. Within your New page, you should have nested fields to create and update the nested models.

In the form, you can use fields_for to access the associated model's records to that event record. In the new action case, we are building 3 questions and each question has 5 answers.

<%= simple_form_for @event do |f|
%>
  <%= f.input :attribute1 %>
  <%= f.input :attribute2 %>
  <%= f.fields_for :questions do |builder|
%>
    <%= render 'questions', f: builder %>
  <% end%>
<% end %>

I generally like to use a partial when doing nested associations, but if you wanted to keep it all in one file (see below), it would looks something like this.

<%= simple_form_for @event do |f|
%>
  <%= f.input :attribute1 %>
  <%= f.input :attribute2 %>
  <%= f.fields_for :questions do |q| %>
    <%= q.input :question_text %>
    <%= q.fields_for :answers do |a| %>
      <%= a.input :answer_text %>
    <% end %>
  <% end%>
<% end %>

In your show action, you may have something like this

@event = Event.find(params[:id])
@questions = @event.questions
@answers = @question.answers if @questions

From here, you have three instance variables assigned and able to access the associated records to your events and questions.


Categories : Ruby On Rails

Related to : Rails deeply nested resources alternative to accessing params
Rails form_tag nested with nested resources
I think, team_plan_charges_path([@team, plan]) should be something like team_plan_charges_path([@team, @plan, @charges]) where @plan and @charges should be in your charges controller, like @plan = @team.plans.new @charges = @plan.charges.new assuming you have a has_many association between Team and Plan and Plan and Charge. I am also assuming that plan is not some method in your helpers and

Categories : Javascript
angular performance and data binding with a deeply nested collection
Angular cannot handle more than ~2000 watches,some browser can do more but that is a good cap. There are 2 possibilities for the slow updates: 1) Are the APIs you are calling withing the Angular life cycle? If so make sure you do a scope.$apply(). 2) You have too many watches. There are code snippets you can find that you run in the console that will spit back the number of watches If they are hi

Categories : Javascript
How to use Rails detect in Rails on incoming Params
you are missing } at the end @received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new', :username => (params[:Text].detect {|original_sender| original_sender.start_with? "@"})) you can also use the below for what you want @received_msg = Message.create(:content => params[:Text], :user_id => user.id, :status => 'new', :username => pa

Categories : Ruby On Rails
Why are the nested resources for Devise authentication not working?
You have to add :operator_id to Devise's permitted parameters Take a look here Essentially, you want to go with following in application_controller.rb: before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) do |u| u.permit(:email, :password, :password_confirmation, :operat

Categories : Ruby On Rails
Rails forms, params and object methods
If you could also pass in @offer object as local all the way to the prices_table partial, you can do %tr %td= check_box_tag(:outer_price_toggler, 1, offer_object.outer_price.persent? || offer_object.outer_price_normal.present?, class: "price-toggler")

Categories : Ruby On Rails
Recently Add
Custom error messages in rails 3
Validation failed: Email can't be blank, Password can't be blank - Not Blank
rbenv Your Ruby version is 2.0.0, but your Gemfile specified 2.1.2 but the only version installed is 2.1.2
How to add an admin restriction feature through Devise to my rails app?
rails 4 routes error with new controller
Rails undefined method for Module
How to do delete link in rails with modules
Why doesn't better_errors work on cloud 9 ide?
Instance Variables in View
Providing a fallback image url based on a boolean value in Rails
Rails Receiving HTTP POST Request
What is the proper way to set S3 creds in Rails Development ENV
How do I show two different partials on every other pageview?
Ruby on Rails: Populate view based on database column values
Wrapping the bootstrap-sass gem in another gem causes asset manifests to break
.where.not with empty array
assign_attributes and save later in loop - ruby rails
File from a Delayed Job to Uploader
mongoid validate uniqueness of each member in has_many or has_and_belongs_to_many relation
Gibbon Mailchimp integration simply not working
No terminal output with caypbara / rspec / selenium-webdriver
Rack::ShowExceptions enabled in production Rails 4.0.4
Ruby on Rails access whole params hash
Can I view Rails documentation with pry?
Why isn't the confirm box working?
Polymorphic models (not associations!) in Rails
Link_to :Action won't call Controller Method -- Rails 4
Missing template when rendering a collection using local variables
rails simple_form using virtual attributes
uninitialized constant ActiveSupport::TestCase
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.