How to Define Action With Simple Form For

How to define action with simple form for?

Is there a reason you're not using REST for this? It would make your life a lot easier and requires much less code. If you're set on using this custom action, you will need to specify the url and probably the method:

<%= simple_form_for @photographer, :url => savenew_photographers_path, :method => :post ... # etc

simple_form - how to specify form's action attribute

<%= simple_form_for @user, url: '/webinars' do %>
...
<% end %>

simple_form_for with custom controller action

Your route below:

user_change_client PATCH  /users/:user_id/change_client(.:format)

Requires a user_id parameter. When you call this helper, pass your current user to it like so:

user_change_client_path(current_user)

Simple_form_for action hitting wrong URL on edit in rails

As you are using common _form for new and update, you need to add condition in url respectively,

<%= simple_form_for @post, url:(@post.new_record? ? newsfeeds_path : newsfeed_path(@post)) do |f| %>

This will help you using both new and update method.
Hope this will help.

Use the same form on the same page for new and edit actions (simple_form & Rails)

Solution:

Regarding

... but when trying to edit Rails raises the DosesController#edit is missing a template for this request format and variant

... means that you are missing the file app/views/doses/edit.html.erb. So just create that file, or... because you said...

I guess the problem is that I'm using a cocktails/:id/doses/:id route, but displaying the form on a cocktails/:id/show view

... which if understood it correctly, you want this url cocktails/:id/doses/:id to show a page exactly as your cocktails/:id/ (which presumably mapped to your cocktails#show page, then you can just do the following:

app/controllers/doses_controller.rb:

# ...
def edit
@cocktail = Cocktail.find(params[:id])
@dose = Dose.find(params[:dose_id])

render 'cocktails/show'

# rails by default has an invisible line at the end of the actions, unless `render` is called manually like line above. But removing the line above, you'll get something like below
# render 'CONTROLLER_NAME/ACTION_NAME.FORMAT.TEMPLATE_ENGINE'
# so in this specific case:
# CONTROLLER_NAME == doses
# ACTION_NAME == edit
# FORMAT == html
# TEMPLATE_ENGINE == erb
# which translates to:
# render 'doses/edit.html.erb'
# which you can also do like the following
# render 'edit.html.erb' # because same controller name as the template being called
# which you can also do like the following
# render 'edit' # because it automatically identifies what format using the value of `request.format`, and what default template engine your Rails app is configured to use
# ... and this is the reason why you got that error "DosesController#edit is missing a template...", because app/views/doses/edit.html.erb does not exist, which by default is being rendered

Recommendation:

  • Please clarify first why you want the following route, and I'll see if I have a better solution, in my opinion:

    get "cocktails/:id/doses/:dose_id", to: "doses#edit", as: :new_or_edit_dose

... I asked this because normally a "create or update" / "new or edit" request is routed against some unique attribute identifier. For example, in your case, the unique attribute identifier seems to be :dose_id, because you "create" a new Dose record if the :dose_id does not yet exist, or you "update" the Dose record if it already exists which has the id value equal to :dose_id. Now, this is my opinion but I do not think it is a good idea to create a Dose record from a manually-set "id", because you normally let the database auto-assign the primary key id values.

...To explain my point, for example, to simulate the said route above, say I open this URL in the browser: "/cocktails/1/doses/1", so far so good assuming that Dose(id: 1) already exists, then I will just see on the page a form to UPDATE the Dose. However, let's say I open "/cocktails/1/doses/9999999999", and let's say that Dose(id: 9999999999) does not exist yet, then I will see a form to CREATE the Dose... and this is still valid at this point. The problem occurs when you already deleted the Dose record. So let's say, Dose(id: 1) has already been deleted, and then I tried opening "/cocktails/1/doses/1" again, because it's already been deleted, then I will see a form to CREATE the Dose (which is still ok), but becomes problematic because you are reusing the id values which should be a "primary key" and should uniquely identify a resource, because your associations / models that depend on this unique id value would potentially be messed up: like for example I have a Car record of attributes {name: 'Porsche', category_id: 23} that belongs_to a Category(23) record that has an attribute {name: 'vehicle'}, and then somehow out of the blue Category(23) has been destroyed, and then recreated with the same ID! but this time the attributes created let's say has been become {name: 'fruit'}. Now, my Porsche Car becomes a Fruit!!

However, I can think of some reasons why you'd intentionally do this (i.e. if you want to treat the id value as uuid which can potentially never be reused programatically), but my thoughts above just in case you are not aware of yet with these possible consequences.

Call a Model Method with simple_form Submit in rails

Your method receives a parameter (shipping) but it is not using it:

def update_order_from_shipping_page(shipping)
new_total = self.total + self.shipping
self.update_attributes(total: new_total)
end

new_total is adding self.shipping to self.total, instead of adding shipping. So, unless self.shipping already contains any data, it will not add anything.

As a result, when you call that method with:

@order.update_order_from_shipping_page(shipping)

it is not taking into account shipping and no update to total is done.

To fix it, change update_order_from_shipping_page method so it adds shipping instead of self.shipping:

def update_order_from_shipping_page(shipping)
new_total = self.total + shipping
self.update_attributes(total: new_total)
end

UPDATE

To avoid Array can't be coerced into BigDecimal, you need to get the correct value from the options Array and convert it to Integer/Float. To accomplish that update your controller's update_order method:

def update_order
@order = current_order
shipping = params[:order][:shipping].gsub(/[$,]/,""​).to_f # new line added

if @order.update_order_from_shipping_page(shipping)
redirect_to new_charge_path and return
else
redirect_to :back
flash[:notice] = "Something is amuck."
end
end
  • gsub(/[$,]/,""​) is to remove currency characters ($ and ,)

  • to_f to convert String to Float.

Rails simple_form_for update action routing error [POST] with id

config.api_only = false allow Middleware to Rack::MethodOverride

I have config.api_only = true and that's was my problem.



Related Topics



Leave a reply



Submit