开发者

Groupon Alert Rails

开发者 https://www.devze.com 2023-02-16 01:09 出处:网络
I am new to rails so please be patient. I am open to suggestions on how to do this differently. I would like to render 2 separate controller actions in one layout in rails.

I am new to rails so please be patient. I am open to suggestions on how to do this differently.

I would like to render 2 separate controller actions in one layout in rails.

I have 2 controllers: Coupons, and MainAlert. In the b开发者_JAVA百科ody of my application wide layout page I have a <yield > which loads the index action of Coupons or MainAlerts depending on the request (e.i. localhost/coupons or localhost/MainAlerts).

However, I would like to load the index action of Coupons or MainAlert or other controller (depending on request) but ALWAYS load the _form(where user creates new alert) at the very top on a I will hide and show.

"Get deals by email (+)" option at groupon.com

How do I load both controller action (index from Coupons and the _form (new? create?) from MainAlerts in the layout. The values of the MainAlert form need to be save to the DB if user hits submit.

I am open to suggestions on how to do this differently.

Thank you for you time everyone. =)


I'm kind of a newbie too, so expect more nifty answers.

But one way to solve this would be to use an before_filter in the Application Controller to always set up a new MainCoupon instance variable as every action is serviced. After that you could use render 'maincoupon/form' to render the form in the layout. The form should work as intended because the need instance variable was set up by the before_filter.

It could look something like this:

# application_controller.rb
class ApplicationController < ActionController::Base
  # other stuff
  before_filter :new_coupon


  # other stuff


  def new_coupon
    @maincoupon = MainCoupon.new
  end
end

In the layout you could have

<% = render 'maincoupons/form' %>

Or better yet, using HAML, just:

= render 'maincoupons/form'

In general your new action is associated with a view where the user enters information into a form. The new action in the controller creates a new object @maincoupon = MainCoupon.new of the desired to type, which is used as a "scaffold" for building the form.

When the user submits, the form information is packaged sent as a parameter to the create action, which takes the information sent from the form and uses it to create a new object of the desired type. @maincoupon = MainCoupon.new(params[:maincoupon])

After that it uses the @maincoupon.save method to save it the to the database. You can try the corresponding model methods out yourself in the console (rails console). For example:

> A = User.new

Would create a new user, but doesn't save it to the db. You could continue like this:

> A.name = "Apa"
> A.save

This will create and save it straight away.

> User.create(:name => "Apa")
0

精彩评论

暂无评论...
验证码 换一张
取 消