Ken W
Ken W

Reputation: 962

How/Where to appropriately call controller create method

I just sorted out my inter-model relations regarding has_many :through and belongs_to; I also added the required id fields to the database table using a migration.

My question is: after user clicks on the link:

<%= link_to "subscribe", new_subscription_path(feed_url: @feed.feed_url)%>

I do this in my new method:

def new
    feed_url = params[:feed_url]
    @subscription = Subscription.new

    redirect_to reader_url, notice: "You are now subscribed to: "+Feed.find_by_feed_url(feed_url).title
  end

I just can't figure out exactly how and where I should be calling my create method since I want the subscribe link to create a new row in my subscriptions table.

Also to make sure that my tables are correct here are my associations:

User has_many :feeds, :through => :subscriptions, dependent: :destroy

|Users table has column: id

Subscription belongs_to :feed
Subscription belongs_to :user

|Subscriptions table has columns: id, user_id, feed_id

Feed has_many :users, :through => :subscriptions

|Feeds table has column: id

Upvotes: 0

Views: 197

Answers (1)

jdoe
jdoe

Reputation: 15771

You've just broken the whole idea of REST )

The new actions is made to show a user some form for filling details of the resource that is being created. Even HTTP-verb GET (that you can see in your log for you new action) says that it starts an action that should NOT modify any resource.

If you don't need any forms, you can create direct "accessor" to your create action. But don't make it via link_to since your users will be unable to click on it properly without javascript enabled. Use button_to:

button_to 'Create', resources_path(your_params)

And then define the creation itself inside your create action.

Upvotes: 4

Related Questions