mardi 28 février 2017

Using Partial View's Controller Instead

I'm trying to use partial view's controller instead of the view where I render it. My scenario is as following;

I have users and users can post something. Thats why I've two controller and models (users and post).

In the user's page, there is a textarea to get input of content and button to post . I've written this view .../views/posts/_new.html.erb as partial view and I render this partial in the .../views/users/index.html.erb and to use partial view I've used this piece of code.

<%= render 'posts/new' %>

In order to create post, I've written this method;

posts_controller.rb

  def new
    @post = Post.new
  end

  def edit
    @post = Post.find(params[:id])
  end

  def create
    @user = User.find(params[:user_id])
    @post = @user.posts.create(post_params)

    if @post.save
      redirect_to root_path
    else
      render 'new'
    end
  end

.../views/posts/_new.html.erb

<%= form_for :post do |f| %>
          <%= f.text_area :content, :class => "form-control", "rows" => "5", "id" => "comment", "placeholder" => "Write something to share with your mates ..." %><br/>
          <%= button_tag  "Share", :class => "btn btn-primary pull-right" do %>
            <span class="glyphicon glyphicon-share-alt"></span>
          <% end %>
        <% end %>

The problem here is, when I click the button in partial view, it goes to Users#RegistrationController of devise controller. Why ?

Here, guide tells that if there is a relationship between partials and the view that renders it, rails understands what we want to do and and use partial view's controller instead. Am I missing something here ?

Aucun commentaire:

Enregistrer un commentaire