Rails -- Create and Create! Methods, Ror 3 Tutorial

Rails -- create and create! methods, RoR 3 Tutorial

Though a lot of classes treat bang methods as "a method that modifies the object in place", I like the description of bang methods from the Eloquent Ruby book better:

In practice, Ruby programmers reserve
! to adorn the names of methods that do
something unexpected, or perhaps a bit
dangerous

So in this case, the "unexpected" result is that an exception is raised instead of just failing and returning false.

Rails: Difference between create and new methods in ActiveRecord?

Basically the new method creates an object instance and the create method additionally tries to save it to the database if it is possible.

Check the ActiveRecord::Base documentation:

create method
Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.

new method
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names).

Create automatically methods

You can do this using define_singleton_method:

class Worker; end
class Truck; end

class House
def self.building_steps
[
[Worker, :buy_material],
[Worker, :blend_material],
[Truck, :remove_ground]
]
end

def self.check_process(klass, method)
"executing #{method}"
end

building_steps.each do |klass, method|
define_singleton_method(method) do
check_process(klass, method)
end
end
end

puts House.buy_material #=> executing buy_material

Create method for nested resource with protected ID attribute

If you are getting project via params[:project_id] rather than params[:role][:project_id] you could actually be setting conflicting values anyway.

The reason Mass Assignment would want to protect this is to prevent a user entering in an arbitrary value for project_id that could allow a project that isn't under this users control. You have a couple of options.

If you had an authorative user or account attached to the object you could add in a before_save callback, such as self.project_id = nil unless user.projects.find(project_id).

Since you don’t, I'd use the project_id from the hash to find the project, and fall back to the route id (I'm not sure if it would be project_id or just id off the top of my head).

def create
user.
projects.
find(params[:role].delete(:project_id) || params[:project_id] || params[:id]).
create(params[:role])

The easiest thing would be to just drop the select box from the form, since they've selected a project when choosing to create a new role – it's a nested resource.

Following Rails tutorial, undefined method 'create' for nil:NilClass, with nested resource

@maguri, that's not all necessary. The stumbling block I was running into was that Rails couldn't automatically determine the correct routes. When I provided my own urls in the form_with declarations, everything went smoothly.

Observe the following change in my _form.html.erb for the Insured model, which belongs_to Policy, which has_one Insured.

<%= form_with model: @insured, url: policy_insured_path(@policy) local: true do |form| %>

In my updated insureds_controller.rb file, using @Phlip's suggestion:

def create
@policy = Policy.find(params[:policy_id])
@insured = @policy.create_insured(insured_params)
if @policy.insured.save
redirect_to policy_insured_path(params[:policy_id])
else
render 'new'
end
end

This allows me to keep routes.rb clean and simple:

resources :policies do
resource: insured
end

Thank you for your answer, it helped me discover the problem was with my routes.

ruby on rails use create method in controller as GET not working

You probably configured the create as a POST not a GET.

  # POST /friendships
# POST /friendships.json
def create

This is also the case if you used scaffolding to create a skeleton of your controller. You could change this in the routes configuration. But be aware, that creating stuff via GET is no longer seen as completely conform to the REST paradigm.

Ch. 10.3.3 Ruby on Rails Tutorial Undefined Method 'any?'

@feed_items is nil, and you're trying to call any? on it. Nil doesn't have that method.

Your controller is "home", but your view is "index". It may be that your controller action isn't being run, and that @feed_items isn't being populated as a result. Try renaming home to index and see if that fixes it.

Ruby on Rails - Create polymorphic comment through partial

When you pass a record to a form builder rails uses the polymorphic route helpers* to lookup the url for the action attribute.

To route to a nested resource you need to pass the parent and child(ren) in an array:

bootstrap_form_for([@commentable, @comment])
# or
bootstrap_form_for([@comment.commentable, @comment])

This would give the path /images/:image_id/comments for a new record and /images/:image_id/comments/:id if it has been persisted.



Related Topics



Leave a reply



Submit